Bean Validation Constraint Challenge
The Bean Validation 2.0 API introduces new constraint. Which of the following are among those new constraints?
The new constraints are @NotBlank, @Email, @NegativeOrZero and @PastOrPresent.
Bean Validation 2.0
Bean Validation 2.0 includes a range of new features. Among them are constraints that validate email addresses, ensure that numbers are positive or negative, test if dates are past or present, and test that fields are not empty or null. These are @Email, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent, @FutureOrPresent, @NotEmpty, and @NotBlank.
How to Use These Constraints
Constraints act in a wider range of locations than in previous API releases. They can proceed arguments of parameterized types and support for validating container elements by type arguments has been added as in the following code snippet.
List<@Size(min = 30) String> favourites;
Other Bean Validation Enhancements
Cascaded Validation of Containers
Cascaded validation of containers has been added. If you annotate any type argument of a container with @Valid it will cause the validation of every element in that container when the parent object is validated. In the code snippet below, every Driver and Car element will be validated as shown:
Map<@Valid Driver, @Valid Car> driverAndCar;
Custom Container Types
Bean Validation also adds support for custom container types by plugging in value extractors. Built-in constraints are marked as repeatable, parameter names are retrieved using reflection, andConstraintValidator#initialize() is a default method. JavaFX also gets support for its types.
Java SE 8 Uplift
The API gets an uplift to Java SE 8’s Date and Time types and offers support for java.util.Optional.
For further information on how to use Bean Validation’s new features please take a look at my article What’s new in Java EE 8 over at IBM developerWorks and also you should read the specifications for the Bean Validation 2.0 (JSR 380) API.
The Original Tweet
— Alex Theedom (@alextheedom) October 10, 2017