public class BeanValidator extends Object implements Validator<Object>
Validator
using the JSR-303 (javax.validation) annotation-based
bean validation mechanism. Values passed to this validator are compared
against the constraints, if any, specified by annotations on the
corresponding bean property.
Note that a JSR-303 implementation (for instance Hibernate Validator or Apache BVal) must be present on the project classpath when using bean validation. Specification versions 1.0 and 1.1 are supported.
Constructor and Description |
---|
BeanValidator(Class<?> beanType,
String propertyName)
Creates a new JSR-303
BeanValidator that validates values of the
specified property. |
Modifier and Type | Method and Description |
---|---|
ValidationResult |
apply(Object value,
ValueContext context)
Validates the given value as if it were the value of the bean property
configured for this validator.
|
protected javax.validation.MessageInterpolator.Context |
createContext(javax.validation.ConstraintViolation<?> violation)
Creates a simple message interpolation context based on the given
constraint violation.
|
javax.validation.Validator |
getJavaxBeanValidator()
Returns a shared JSR-303 validator instance to use.
|
protected static javax.validation.ValidatorFactory |
getJavaxBeanValidatorFactory()
Returns the underlying JSR-303 bean validator factory used.
|
protected String |
getMessage(javax.validation.ConstraintViolation<?> violation,
Locale locale)
Returns the interpolated error message for the given constraint violation
using the locale specified for this validator.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
alwaysPass, from, from, from, from
andThen
public BeanValidator(Class<?> beanType, String propertyName)
BeanValidator
that validates values of the
specified property. Localizes validation messages using the
default locale.beanType
- the bean type declaring the property, not nullpropertyName
- the property to validate, not nullIllegalStateException
- if BeanUtil.checkBeanValidationAvailable()
returns
falsepublic ValidationResult apply(Object value, ValueContext context)
Result.ok
if there are no
JSR-303 constraint violations, a Result.error
of chained
constraint violation messages otherwise.
Null values are accepted unless the property has an @NotNull
annotation or equivalent.
apply
in interface Validator<Object>
apply
in interface BiFunction<Object,ValueContext,ValidationResult>
value
- the input value to validatecontext
- the value context for validationprotected static javax.validation.ValidatorFactory getJavaxBeanValidatorFactory()
Validation
if necessary.public javax.validation.Validator getJavaxBeanValidator()
protected String getMessage(javax.validation.ConstraintViolation<?> violation, Locale locale)
violation
- the constraint violationlocale
- the used localeprotected javax.validation.MessageInterpolator.Context createContext(javax.validation.ConstraintViolation<?> violation)
violation
- the constraint violationCopyright © 2019. All rights reserved.