The main theme of Bean Validation 2.0 is support for and taking advantage of Java 8.
This concerns new language features such as type use annotations as well as library additions.
An example of the latter is the support for the new Java 8 date and time API (JSR 310).
You can now use
javax.time types such as
private LocalDate deliveryDate;
For types that don’t represent a specific instant but rather an interval of time,
you can configure that the current month, year etc. should be considered valid, too,
using the new attribute
private final Year inceptionYear = Year.of( 2017 );
An example where Bean Validation benefits from new language features in Java 8
is the new mechanism for validating the elements of
Optional and other container types.
By annotating type arguments of generic types you can now put constraints to the container elements
(as opposed to the container itself):
List<@NotNull @Email String> emails;
Also cascaded validation (
@Valid) gets more powerful with that.
E.g. you can now perform a cascaded validation of map keys and map values (only values were supported before):
Map<@Valid Customer, @Valid Address> primaryAddressByCustomer;
Another use case for this is validation of values wrapped in a
Optional<@Past LocalDate> getRegistrationDate();
We’ve baked in support for type argument constraints on types such as
Optional and some more,
but this isn’t a fixed list.
You can plug in custom implementations of the
which will allow you to put type argument constraints to other collection types (e.g. Google Guava’s
or even collection classes from other JVM languages such as Ceylon.