public class SpringValidatorAdapter extends Object implements SmartValidator, Validator
javax.validator.Validator
and exposes it as a Spring Validator
while also exposing the original JSR-303 Validator interface itself.
Can be used as a programmatic wrapper. Also serves as base class for
CustomValidatorBean
and LocalValidatorFactoryBean
.
Constructor and Description |
---|
SpringValidatorAdapter(Validator targetValidator)
Create a new SpringValidatorAdapter for the given JSR-303 Validator.
|
Modifier and Type | Method and Description |
---|---|
protected String |
determineErrorCode(ConstraintDescriptor<?> descriptor)
Determine a Spring-reported error code for the given constraint descriptor.
|
protected String |
determineField(ConstraintViolation<Object> violation)
Determine a field for the given constraint violation.
|
protected Object[] |
getArgumentsForConstraint(String objectName,
String field,
ConstraintDescriptor<?> descriptor)
Return FieldError arguments for a validation error on the given field.
|
BeanDescriptor |
getConstraintsForClass(Class<?> clazz) |
protected Object |
getRejectedValue(String field,
ConstraintViolation<Object> violation,
BindingResult bindingResult)
Extract the rejected value behind the given constraint violation,
for exposure through the Spring errors representation.
|
protected void |
processConstraintViolations(Set<ConstraintViolation<Object>> violations,
Errors errors)
Process the given JSR-303 ConstraintViolations, adding corresponding errors to
the provided Spring
Errors object. |
boolean |
supports(Class<?> clazz)
|
<T> T |
unwrap(Class<T> type) |
void |
validate(Object target,
Errors errors)
Validate the supplied
target object, which must be
of a Class for which the Validator.supports(Class) method
typically has (or would) return true . |
void |
validate(Object target,
Errors errors,
Object... validationHints)
Validate the supplied
target object, which must be of a type of Class
for which the Validator.supports(Class) method typically returns true . |
<T> Set<ConstraintViolation<T>> |
validate(T object,
Class<?>... groups) |
<T> Set<ConstraintViolation<T>> |
validateProperty(T object,
String propertyName,
Class<?>... groups) |
<T> Set<ConstraintViolation<T>> |
validateValue(Class<T> beanType,
String propertyName,
Object value,
Class<?>... groups) |
public SpringValidatorAdapter(Validator targetValidator)
targetValidator
- the JSR-303 Validator to wrappublic void validate(Object target, Errors errors)
Validator
target
object, which must be
of a Class
for which the Validator.supports(Class)
method
typically has (or would) return true
.
The supplied errors
instance can be used to report
any resulting validation errors.
target
- the object that is to be validated (can be null
)errors
- contextual state about the validation process (never null
)ValidationUtils
public void validate(Object target, Errors errors, Object... validationHints)
SmartValidator
target
object, which must be of a type of Class
for which the Validator.supports(Class)
method typically returns true
.
The supplied errors
instance can be used to report any
resulting validation errors.
This variant of validate()
supports validation hints, such as
validation groups against a JSR-303 provider (in which case, the provided hint
objects need to be annotation arguments of type Class
).
Note: Validation hints may get ignored by the actual target Validator
,
in which case this method should behave just like its regular
Validator.validate(Object, Errors)
sibling.
validate
in interface SmartValidator
target
- the object that is to be validated (can be null
)errors
- contextual state about the validation process (never null
)validationHints
- one or more hint objects to be passed to the validation engineValidationUtils
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors)
Errors
object.violations
- the JSR-303 ConstraintViolation resultserrors
- the Spring errors object to register toprotected String determineField(ConstraintViolation<Object> violation)
The default implementation returns the stringified property path.
violation
- the current JSR-303 ConstraintViolationErrors
)ConstraintViolation.getPropertyPath()
,
FieldError.getField()
protected String determineErrorCode(ConstraintDescriptor<?> descriptor)
The default implementation returns the simple class name of the descriptor's
annotation type. Note that the configured
MessageCodesResolver
will automatically
generate error code variations which include the object name and the field name.
descriptor
- the JSR-303 ConstraintDescriptor for the current violationErrors
)ConstraintDescriptor.getAnnotation()
,
MessageCodesResolver
protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor<?> descriptor)
The default implementation returns a first argument indicating the field name (of type DefaultMessageSourceResolvable, with "objectName.field" and "field" as codes). Afterwards, it adds all actual constraint annotation attributes (i.e. excluding "message", "groups" and "payload") in alphabetical order of their attribute names.
Can be overridden to e.g. add further attributes from the constraint descriptor.
objectName
- the name of the target objectfield
- the field that caused the binding errordescriptor
- the JSR-303 constraint descriptorDefaultMessageSourceResolvable.getArguments()
,
DefaultMessageSourceResolvable
,
DefaultBindingErrorProcessor.getArgumentsForBindError(java.lang.String, java.lang.String)
protected Object getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult)
field
- the field that caused the binding errorviolation
- the corresponding JSR-303 ConstraintViolationbindingResult
- a Spring BindingResult for the backing object
which contains the current field's valueConstraintViolation.getInvalidValue()
,
FieldError.getRejectedValue()
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups)
public <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups)
validateProperty
in interface Validator
public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)
validateValue
in interface Validator
public BeanDescriptor getConstraintsForClass(Class<?> clazz)
getConstraintsForClass
in interface Validator