Class ConstraintValidatorContextImpl
- All Implemented Interfaces:
ConstraintValidatorContext,HibernateConstraintValidatorContext
- Direct Known Subclasses:
CrossParameterConstraintValidatorContextImpl
- Author:
- Hardy Ferentschik, Gunnar Morling, Guillaume Smet
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classprotected classNested classes/interfaces inherited from interface jakarta.validation.ConstraintValidatorContext
ConstraintValidatorContext.ConstraintViolationBuilder -
Constructor Summary
ConstructorsConstructorDescriptionConstraintValidatorContextImpl(ClockProvider clockProvider, PathImpl propertyPath, ConstraintDescriptor<?> constraintDescriptor, Object constraintValidatorPayload, ExpressionLanguageFeatureLevel defaultConstraintExpressionLanguageFeatureLevel, ExpressionLanguageFeatureLevel defaultCustomViolationExpressionLanguageFeatureLevel) -
Method Summary
Modifier and TypeMethodDescriptionaddExpressionVariable(String name, Object value) Allows to set an additional expression variable which will be available as an EL variable during interpolation.addMessageParameter(String name, Object value) Allows to set an additional named parameter which can be interpolated in the constraint violation message.buildConstraintViolationWithTemplate(String messageTemplate) Returns a constraint violation builder building a violation report allowing to optionally associate it to a sub path.final voidDisables the defaultConstraintViolationobject generation (which is using the message template declared on the constraint).Returns the provider for obtaining the current time in the form of aClock, e.g. when validating theFutureandPastconstraints.final ConstraintDescriptor<?><C> CgetConstraintValidatorPayload(Class<C> type) Returns an instance of the specified type ornullif the current constraint validator payload isn't of the given type.protected final PathImplfinal String<T> TReturns an instance of the specified type allowing access to provider-specific APIs.withDynamicPayload(Object violationContext) Allows to set an object that may further describe the violation.
-
Constructor Details
-
ConstraintValidatorContextImpl
public ConstraintValidatorContextImpl(ClockProvider clockProvider, PathImpl propertyPath, ConstraintDescriptor<?> constraintDescriptor, Object constraintValidatorPayload, ExpressionLanguageFeatureLevel defaultConstraintExpressionLanguageFeatureLevel, ExpressionLanguageFeatureLevel defaultCustomViolationExpressionLanguageFeatureLevel)
-
-
Method Details
-
disableDefaultConstraintViolation
public final void disableDefaultConstraintViolation()Description copied from interface:ConstraintValidatorContextDisables the defaultConstraintViolationobject generation (which is using the message template declared on the constraint).Useful to set a different violation message or generate a
ConstraintViolationbased on a different property.- Specified by:
disableDefaultConstraintViolationin interfaceConstraintValidatorContext
-
getDefaultConstraintMessageTemplate
- Specified by:
getDefaultConstraintMessageTemplatein interfaceConstraintValidatorContext- Returns:
- the current un-interpolated default message
-
buildConstraintViolationWithTemplate
public HibernateConstraintViolationBuilder buildConstraintViolationWithTemplate(String messageTemplate) Description copied from interface:ConstraintValidatorContextReturns a constraint violation builder building a violation report allowing to optionally associate it to a sub path. The violation message will be interpolated.To create the
ConstraintViolation, one must call either one of theaddConstraintViolation()methods available in one of the interfaces of the fluent API. If another method is called afteraddConstraintViolation()onConstraintViolationBuilderor any of its associated nested interfaces anIllegalStateExceptionis raised.If
ConstraintValidator.isValid(Object, ConstraintValidatorContext)returnsfalse, aConstraintViolationobject will be built per constraint violation report including the default one (unlessConstraintValidatorContext.disableDefaultConstraintViolation()has been called).ConstraintViolationobjects generated from such a call contain the same contextual information (root bean, path and so on) unless the path has been overridden.To create a different
ConstraintViolation, a new constraint violation builder has to be retrieved fromConstraintValidatorContextHere are a few usage examples://assuming the following domain model public class User { public Map<String,Address> getAddresses() { ... } } public class Address { public String getStreet() { ... } public Country getCountry() { ... } } public class Country { public String getName() { ... } } //From a property-level constraint on User.addresses //Build a constraint violation on the default path - i.e. the "addresses" property context.buildConstraintViolationWithTemplate( "this detail is wrong" ) .addConstraintViolation(); //From a class level constraint on Address //Build a constraint violation on the default path + "street" //i.e. the street property of Address context.buildConstraintViolationWithTemplate( "this detail is wrong" ) .addPropertyNode( "street" ) .addConstraintViolation(); //From a property-level constraint on User.addresses //Build a constraint violation on the default path + the bean stored //under the "home" key in the map context.buildConstraintViolationWithTemplate( "Incorrect home address" ) .addBeanNode() .inContainer( Map.class, 1 ) .inIterable().atKey( "home" ) .addConstraintViolation(); //From a class level constraint on User //Build a constraint violation on the default path + addresses["home"].country.name //i.e. property "country.name" on the object stored under "home" in the map context.buildConstraintViolationWithTemplate( "this detail is wrong" ) .addPropertyNode( "addresses" ) .addPropertyNode( "country" ) .inContainer( Map.class, 1 ) .inIterable().atKey( "home" ) .addPropertyNode( "name" ) .addConstraintViolation(); //From a class level constraint on User //Build a constraint violation on the default path + addresses["home"].<map key> //i.e. a container element constraint violation for the map key context.buildConstraintViolationWithTemplate( "the map key is invalid" ) .addPropertyNode( "addresses" ) .addContainerElementNode( "<map key>", Map.class, 0 ) .inIterable().atKey( "invalid" ) .addConstraintViolation();Cross-parameter constraints on a method can create a node specific to a particular parameter if required. Let's explore a few examples:
//Cross-parameter constraint on method //createUser(String password, String passwordRepeat) //Build a constraint violation on the default path + "passwordRepeat" context.buildConstraintViolationWithTemplate("Passwords do not match") .addParameterNode(1) .addConstraintViolation(); //Cross-parameter constraint on a method //mergeAddresses(Map<String,Address> addresses, // Map<String,Address> otherAddresses) //Build a constraint violation on the default path + "otherAddresses["home"] //i.e. the Address bean hosted in the "home" key of the "otherAddresses" map parameter context.buildConstraintViolationWithTemplate( "Map entry home present in both and does not match") .addParameterNode(1) .addBeanNode() .inContainer( Map.class, 1 ) .inIterable().atKey("home") .addConstraintViolation(); //Cross-parameter constraint on a method //mergeAddresses(Map<String,Address> addresses, // Map<String,Address> otherAddresses) //Build a constraint violation on the default path + "otherAddresses["home"].city //i.e. on the "city" property of the Address bean hosted in //the "home" key of the "otherAddresses" map context.buildConstraintViolationWithTemplate( "Map entry home present in both but city does not match") .addParameterNode(1) .addPropertyNode("city") .inContainer( Map.class, 1 ) .inIterable().atKey("home") .addConstraintViolation();- Specified by:
buildConstraintViolationWithTemplatein interfaceConstraintValidatorContext- Specified by:
buildConstraintViolationWithTemplatein interfaceHibernateConstraintValidatorContext- Parameters:
messageTemplate- new un-interpolated constraint message- Returns:
- returns a constraint violation builder
-
unwrap
Description copied from interface:ConstraintValidatorContextReturns an instance of the specified type allowing access to provider-specific APIs. If the Jakarta Bean Validation provider implementation does not support the specified class,ValidationExceptionis thrown.- Specified by:
unwrapin interfaceConstraintValidatorContext- Type Parameters:
T- the type of the object to be returned- Parameters:
type- the class of the object to be returned- Returns:
- an instance of the specified class
-
addExpressionVariable
Description copied from interface:HibernateConstraintValidatorContextAllows to set an additional expression variable which will be available as an EL variable during interpolation. The variable will be available for interpolation for all constraint violations generated for this constraint. This includes the default one as well as all violations created by theConstraintValidatorContext.ConstraintViolationBuilder. To create multiple constraint violations with different variable values, this method can be called between successive calls toConstraintValidatorContext.ConstraintViolationBuilder.addConstraintViolation().For example:
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class ); context.addExpressionVariable( "foo", "bar" ); context.buildConstraintViolationWithTemplate( "${foo}" ) .addConstraintViolation(); context.addExpressionVariable( "foo", "snafu" ); context.buildConstraintViolationWithTemplate( "${foo}" ) .addConstraintViolation(); return false; }- Specified by:
addExpressionVariablein interfaceHibernateConstraintValidatorContext- Parameters:
name- the name under which to bind the expression variable, cannot benullvalue- the value to be bound to the specified name- Returns:
- a reference to itself to allow method chaining
-
addMessageParameter
Description copied from interface:HibernateConstraintValidatorContextAllows to set an additional named parameter which can be interpolated in the constraint violation message. The variable will be available for interpolation for all constraint violations generated for this constraint. This includes the default one as well as all violations created by theConstraintValidatorContext.ConstraintViolationBuilder. To create multiple constraint violations with different variable values, this method can be called between successive calls toConstraintValidatorContext.ConstraintViolationBuilder.addConstraintViolation().For example:
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class ); context.addMessageParameter( "foo", "bar" ); context.buildConstraintViolationWithTemplate( "{foo}" ) .addConstraintViolation(); context.addMessageParameter( "foo", "snafu" ); context.buildConstraintViolationWithTemplate( "{foo}" ) .addConstraintViolation(); return false; }- Specified by:
addMessageParameterin interfaceHibernateConstraintValidatorContext- Parameters:
name- the name under which to bind the parameter, cannot benullvalue- the value to be bound to the specified name- Returns:
- a reference to itself to allow method chaining
-
getClockProvider
Description copied from interface:ConstraintValidatorContextReturns the provider for obtaining the current time in the form of aClock, e.g. when validating theFutureandPastconstraints.- Specified by:
getClockProviderin interfaceConstraintValidatorContext- Returns:
- the provider for obtaining the current time, never
null. If no specific provider has been configured during bootstrap, a default implementation using the current system time and the current default time zone as returned byClock.systemDefaultZone()will be returned.
-
withDynamicPayload
Description copied from interface:HibernateConstraintValidatorContextAllows to set an object that may further describe the violation. The user is responsible himself to ensure that this payload is serializable in case thejakarta.validation.ConstraintViolationhas to be serialized.- Specified by:
withDynamicPayloadin interfaceHibernateConstraintValidatorContext- Parameters:
violationContext- an object representing additional information about the violation- Returns:
- a reference to itself to allow method chaining
-
getConstraintValidatorPayload
Description copied from interface:HibernateConstraintValidatorContextReturns an instance of the specified type ornullif the current constraint validator payload isn't of the given type.- Specified by:
getConstraintValidatorPayloadin interfaceHibernateConstraintValidatorContext- Parameters:
type- the type of payload to retrieve- Returns:
- an instance of the specified type or
nullif the current constraint validator payload isn't of the given type - See Also:
-
getConstraintDescriptor
-
getConstraintViolationCreationContexts
-
getCopyOfBasePath
-