validation library
Bean validation and data binding: constraint annotations such as
@NotBlank() and @Min(18) for the properties of your classes, the
validator that checks them, and the error objects that say what failed
and why.
Import it (the package's main library exports it too) to put rules on
request bodies, forms, @ConfigurationProperties classes and service
methods instead of writing if checks by hand. In an application the
checks usually run without a call of your own:
@Valid()on a controller's@RequestBodyparameter checks the body; a violation is answered with a 400 that lists the fields.@Validated()on a bean class checks the arguments and return values of its methods; a violation throws aConstraintViolationException.@Validated()on a@ConfigurationPropertiesclass checks the bound values, and an invalid one stops the start.
Call BeanValidator yourself for anything else, and use DataBinder
to copy string input (a form, a query) onto an object and check it in
one step:
@Introspected()
class Signup {
@NotBlank()
String name = '';
}
// The empty name breaks @NotBlank: one field error on `name`.
final binder = DataBinder(Signup(), objectName: 'signup')
..validators.add(BeanValidator())
..bind({'name': ''})
..validate();
binder.bindingResult.hasErrors; // true: NotBlank.signup.name
Spring (Java): org.springframework.validation plus a Jakarta Bean
Validation style set of constraints.
class Signup { @NotBlank String name; }
var binder = new DataBinder(new Signup(), "signup");
binder.setValidator(new LocalValidatorFactoryBean());
binder.bind(new MutablePropertyValues(Map.of("name", "")));
binder.validate();
binder.getBindingResult().hasErrors();
Classes
- AbstractBindingResult
- The common part of every BindingResult: the error list, nested paths, message codes and the field queries.
- AssertFalse
-
A constraint that requires a
boolto befalse;nullpasses. - AssertTrue
-
A constraint that requires a
boolto betrue;nullpasses. - BeanPropertyBindingResult
-
The BindingResult for an
@Introspectedobject, the one used almost everywhere. - BeanValidator
-
The validator: it checks an object against the constraint annotations
on its class (
@NotBlank(),@Min(18), custom Constraints) and reports what is broken. - BindingResult
- The outcome of binding input onto an object and validating it: the Errors together with the object they are about.
- ClockProvider
-
Where the date constraints (
@Past(),@FutureDate(), ...) get the current time. - Constraint
-
The base class of every validation rule written as an annotation, such
as
@NotBlank()or@Min(18), and the class to extend for your own. - ConstraintValidatorContext
- The help a constraint gets while it checks one value: the validator's clock, and a way to report violations other than the default one.
- ConstraintViolation
- One broken rule: which constraint failed, where (the property path), on which value, and the finished message.
- ConstraintViolationBuilder
- A violation being put together inside a constraint: its message is set, a property path may be added, and addConstraintViolation reports it.
- ConvertGroup
-
An annotation next to
@Valid()that checks the nested value with another group: when the outer validation runs group from, the nested value is checked with group to. - DataBinder
-
Copies input values, usually strings from a form or a query, onto the
properties of an
@Introspectedobject, then validates it, collecting every problem in one bindingResult. - DecimalMax
- A constraint that requires a number below a decimal limit, or equal to it when inclusive.
- DecimalMin
- A constraint that requires a number above a decimal limit, or equal to it when inclusive.
- Default
-
The validation group of every constraint declared without
groups. - DefaultClockProvider
-
The ClockProvider of the system clock,
DateTime.now(); the default ofBeanValidator. - DefaultMessageCodesResolver
- The standard message codes of errors, the ones every binding result and validator uses unless told otherwise.
- Digits
- A constraint that limits how many digits a number has before and after the decimal point.
- Each
- An annotation that checks every element of a list or set, or every value of a map, against constraints.
- EachKey
- An annotation that checks every key of a map against constraints.
- A constraint that requires a string to look like an email address.
- Errors
- The list of errors found for one object, and the way a validator records them.
- FieldError
- A validation or binding error about one field of an object: which field, the value that was rejected, and the message codes.
- FutureDate
-
A constraint that requires a
DateTimeafter now. - FutureOrPresent
-
A constraint that requires a
DateTimethat is not in the past. - GroupSequence
- A class annotation that checks the class's rules in stages: the groups in value are checked in order, and checking stops at the first group with a violation.
- MapBindingResult
- A BindingResult for values held in a map instead of an object.
- MapValueResolver
- A ValueResolver that reads the values from a map keyed by parameter path, the one to use with DataBinder.construct in most cases.
- Matches
- A constraint that requires a string to match the regular expression regexp as a whole.
- Max
- A constraint that requires a number of at most value.
- MessageCodesResolver
- The rule that turns an error code and a field into the list of message codes an error carries.
- MethodValidationInterceptor
- The AOP advice that checks the arguments and the return value of a call against the method's constraints.
- MethodValidationPostProcessor
-
The bean that turns on method validation: it applies a
MethodValidationInterceptor to every bean whose class is annotated
@Validated(). - Min
- A constraint that requires a number of at least value.
- MustBeNull
-
A constraint that requires the value to be
null, usually in one validation group only. - Negative
- A constraint that requires a number below 0.
- NegativeOrZero
- A constraint that requires a number of 0 or less.
- NotBlank
-
A constraint that requires text with at least one visible character:
null,''and' 'fail. - NotEmpty
-
A constraint that requires a string, list, set or map that is neither
nullnor empty. - NotNull
-
A constraint that requires a value:
nullfails, anything else passes. - ObjectError
- A validation error about a whole object rather than one of its fields, such as "the passwords differ" or a failed class-level constraint.
- Past
-
A constraint that requires a
DateTimebefore now. - PastOrPresent
-
A constraint that requires a
DateTimethat is not in the future. - Positive
- A constraint that requires a number greater than 0.
- PositiveOrZero
- A constraint that requires a number of 0 or more.
- ResolvableAttribute
-
A string attribute of a constraint, such as the regular expression of
@Matches, passed as an argument of a field error. - Size
- A constraint on the length of a string, or the number of entries of a list, set or map: from min to max, both included.
- SmartValidator
- A Validator that can check only the constraints of some validation groups.
- Valid
- An annotation that asks for the constraints inside an object to be checked: of a nested object, of the objects in a list, set or map, or of a request body or method argument.
- Validated
- An annotation that asks for validation, optionally with validation groups.
- ValidationUtils
- Static helpers for writing and running a Validator.
- Validator
- A check of an object written in code: it looks at the object and records each problem in an Errors.
- ValueResolver
- The source of the input values when DataBinder.construct creates an object through its constructor.
Enums
- PatternFlag
- Options for the regular expression of Matches and Email, such as matching without regard to case.
Exceptions / Errors
- BindException
-
The exception
DataBinder.closethrows when binding or validation found errors; it carries them in bindingResult. - ConstraintDeclarationException
-
Thrown when a constraint is declared wrongly: invalid settings
(
Size(min: -1),min > max, a malformed regular expression),@ConvertGroupwithout@Valid(), or@Eachon a value that is not a collection. Jakarta'sConstraintDeclarationException. - ConstraintViolationException
-
The exception a
@Validated()bean throws when a call's arguments or its return value break their constraints. - GroupDefinitionException
-
Thrown when a group sequence is defined wrongly: a cycle,
Defaultin a class's@GroupSequence, the class missing from it, or Default as a key ofBeanValidator.groupSequences. Jakarta'sGroupDefinitionException. - UnexpectedTypeException
-
Thrown when a constraint meets a value of a type it cannot check:
@Sizeon anint,@NotBlankon aList. Jakarta'sUnexpectedTypeException. - ValidationException
- The base of the exceptions that mean validation itself is set up wrongly: a bad constraint declaration or group definition, never an invalid value.