Bean Validation 2.0 added more alignments with Java 8 and CDI, and bring a series of new annotations.
Supports Java 8 Date time and Optional.
Supports annotations applied on parameterized type, such as List<@Email String> emails.
Add a series of new annotations, eg. @Email,@NotEmpty, @NotBlank, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent and @FutureOrPresent.
Let's create a simple example to experience it.
public class Todo implements Serializable {
@NotNull
@NotBlank
private String name;
private String description;
@PastOrPresent
private LocalDateTime creationDate = LocalDateTime.now();
@Future
private LocalDateTime dueDate = LocalDateTime.now().plusDays(2);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDateTime getCreationDate() {
return creationDate;
}
public void setCreationDate(LocalDateTime creationDate) {
this.creationDate = creationDate;
}
public LocalDateTime getDueDate() {
return dueDate;
}
public void setDueDate(LocalDateTime dueDate) {
this.dueDate = dueDate;
}
}
Validator or ValidatorFactory is available for injection, you can inject them into your beans as other CDI beans. validate method will return a collection of ConstraintViolation.
Grab the source codes from my GitHub account, and have a try.