public class SpringValidatorAdapter extends java.lang.Object implements SmartValidator
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
,
and as the primary implementation of the SmartValidator
interface.
As of Spring Framework 5.0, this adapter is fully compatible with Bean Validation 1.1 as well as 2.0.
SmartValidator
,
CustomValidatorBean
,
LocalValidatorFactoryBean
Modifier and Type | Class and Description |
---|---|
private static class |
SpringValidatorAdapter.ResolvableAttribute
Wrapper for a String attribute which can be resolved via a
MessageSource ,
falling back to the original attribute as a default value otherwise. |
private static class |
SpringValidatorAdapter.ViolationFieldError
Subclass of
FieldError with Spring-style default message rendering. |
private static class |
SpringValidatorAdapter.ViolationObjectError
Subclass of
ObjectError with Spring-style default message rendering. |
Modifier and Type | Field and Description |
---|---|
private static java.util.Set<java.lang.String> |
internalAnnotationAttributes |
private javax.validation.Validator |
targetValidator |
Constructor and Description |
---|
SpringValidatorAdapter() |
SpringValidatorAdapter(javax.validation.Validator targetValidator)
Create a new SpringValidatorAdapter for the given JSR-303 Validator.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.Class<?>[] |
asValidationGroups(java.lang.Object... validationHints)
Turn the specified validation hints into JSR-303 validation groups.
|
private static boolean |
containsSpringStylePlaceholder(java.lang.String message) |
protected java.lang.String |
determineErrorCode(<any> descriptor)
Determine a Spring-reported error code for the given constraint descriptor.
|
protected java.lang.String |
determineField(<any> violation)
Determine a field for the given constraint violation.
|
ExecutableValidator |
forExecutables() |
protected java.lang.Object[] |
getArgumentsForConstraint(java.lang.String objectName,
java.lang.String field,
<any> descriptor)
Return FieldError arguments for a validation error on the given field.
|
BeanDescriptor |
getConstraintsForClass(java.lang.Class<?> clazz) |
protected java.lang.Object |
getRejectedValue(java.lang.String field,
<any> violation,
BindingResult bindingResult)
Extract the rejected value behind the given constraint violation,
for exposure through the Spring errors representation.
|
protected MessageSourceResolvable |
getResolvableField(java.lang.String objectName,
java.lang.String field)
Build a resolvable wrapper for the specified field, allowing to resolve the field's
name in a
MessageSource . |
protected void |
processConstraintViolations(java.util.Set<<any>> violations,
Errors errors)
Process the given JSR-303 ConstraintViolations, adding corresponding errors to
the provided Spring
Errors object. |
protected boolean |
requiresMessageFormat(<any> violation)
Indicate whether this violation's interpolated message has remaining
placeholders and therefore requires
MessageFormat
to be applied to it. |
(package private) void |
setTargetValidator(javax.validation.Validator targetValidator) |
boolean |
supports(java.lang.Class<?> clazz)
|
<T> T |
unwrap(java.lang.Class<T> type) |
void |
validate(java.lang.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(java.lang.Object target,
Errors errors,
java.lang.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> java.util.Set<<any>> |
validate(T object,
java.lang.Class<?>... groups) |
<T> java.util.Set<<any>> |
validateProperty(T object,
java.lang.String propertyName,
java.lang.Class<?>... groups) |
void |
validateValue(java.lang.Class<?> targetType,
java.lang.String fieldName,
java.lang.Object value,
Errors errors,
java.lang.Object... validationHints)
Validate the supplied value for the specified field on the target type,
reporting the same validation errors as if the value would be bound to
the field on an instance of the target class.
|
<T> java.util.Set<<any>> |
validateValue(java.lang.Class<T> beanType,
java.lang.String propertyName,
java.lang.Object value,
java.lang.Class<?>... groups) |
private static final java.util.Set<java.lang.String> internalAnnotationAttributes
@Nullable private javax.validation.Validator targetValidator
public SpringValidatorAdapter(javax.validation.Validator targetValidator)
targetValidator
- the JSR-303 Validator to wrapSpringValidatorAdapter()
void setTargetValidator(javax.validation.Validator targetValidator)
public boolean supports(java.lang.Class<?> clazz)
Validator
public void validate(java.lang.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.
validate
in interface Validator
target
- the object that is to be validatederrors
- contextual state about the validation processValidationUtils
public void validate(java.lang.Object target, Errors errors, java.lang.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 validatederrors
- contextual state about the validation processvalidationHints
- one or more hint objects to be passed to the validation enginejavax.validation.Validator#validate(Object, Class[])
public void validateValue(java.lang.Class<?> targetType, java.lang.String fieldName, @Nullable java.lang.Object value, Errors errors, java.lang.Object... validationHints)
SmartValidator
validateValue
in interface SmartValidator
targetType
- the target typefieldName
- the name of the fieldvalue
- the candidate valueerrors
- contextual state about the validation processvalidationHints
- one or more hint objects to be passed to the validation enginejavax.validation.Validator#validateValue(Class, String, Object, Class[])
private java.lang.Class<?>[] asValidationGroups(java.lang.Object... validationHints)
protected void processConstraintViolations(java.util.Set<<any>> violations, Errors errors)
Errors
object.violations
- the JSR-303 ConstraintViolation resultserrors
- the Spring errors object to register toprotected java.lang.String determineField(<any> violation)
The default implementation returns the stringified property path.
violation
- the current JSR-303 ConstraintViolationErrors
)javax.validation.ConstraintViolation#getPropertyPath()
,
FieldError.getField()
protected java.lang.String determineErrorCode(<any> 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
)javax.validation.metadata.ConstraintDescriptor#getAnnotation()
,
MessageCodesResolver
protected java.lang.Object[] getArgumentsForConstraint(java.lang.String objectName, java.lang.String field, <any> descriptor)
The default implementation returns a first argument indicating the field name
(see getResolvableField(java.lang.String, java.lang.String)
). 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 MessageSourceResolvable getResolvableField(java.lang.String objectName, java.lang.String field)
MessageSource
.
The default implementation returns a first argument indicating the field:
of type DefaultMessageSourceResolvable
, with "objectName.field" and "field"
as codes, and with the plain field name as default message.
objectName
- the name of the target objectfield
- the field that caused the binding errorMessageSourceResolvable
for the specified fieldgetArgumentsForConstraint(java.lang.String, java.lang.String, <any>)
@Nullable protected java.lang.Object getRejectedValue(java.lang.String field, <any> 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 valuejavax.validation.ConstraintViolation#getInvalidValue()
,
FieldError.getRejectedValue()
protected boolean requiresMessageFormat(<any> violation)
MessageFormat
to be applied to it. Called for a Bean Validation defined message
(coming out ValidationMessages.properties
) when rendered
as the default message in Spring's MessageSource.
The default implementation considers a Spring-style "{0}" placeholder
for the field name as an indication for MessageFormat
.
Any other placeholder or escape syntax occurrences are typically a
mismatch, coming out of regex pattern values or the like. Note that
standard Bean Validation does not support "{0}" style placeholders at all;
this is a feature typically used in Spring MessageSource resource bundles.
violation
- the Bean Validation constraint violation, including
BV-defined interpolation of named attribute references in its messagetrue
if java.text.MessageFormat
is to be applied,
or false
if the violation's message should be used as-isgetArgumentsForConstraint(java.lang.String, java.lang.String, <any>)
private static boolean containsSpringStylePlaceholder(@Nullable java.lang.String message)
public <T> java.util.Set<<any>> validate(T object, java.lang.Class<?>... groups)
public <T> java.util.Set<<any>> validateProperty(T object, java.lang.String propertyName, java.lang.Class<?>... groups)
public <T> java.util.Set<<any>> validateValue(java.lang.Class<T> beanType, java.lang.String propertyName, java.lang.Object value, java.lang.Class<?>... groups)
public BeanDescriptor getConstraintsForClass(java.lang.Class<?> clazz)
public <T> T unwrap(@Nullable java.lang.Class<T> type)
public ExecutableValidator forExecutables()