Java Bean Validation

JSR 349 is the specification for Java Bean Validation and introduces annotation for validation.

JSR 349 table (excerpt)

The following table is an excerpt of JSR 349 and is extended by the reference implementation: Hibernate Validator 5.x (italic).

BooleanDateStringNumberCollectionsType independent
@AssertFalse@Future@Pattern@DecimalMax@Size@Null
@AssertTrue@Past@Size@DecimalMin@NotEmpty@NotNull
  @Email@Digits  
  @NotBlank@Max  
  @CreditcardNumber@Min  
  @Length   

Explanation

  • @AssertFalse - Property should be false
  • @AssertTrue - Property should be true
  • @Future - Greater than current system time
  • @Past - Lesser than current system time
  • @Pattern - Checks after a specific RegEx
  • @Size - Can define min and max size on Strings or Collections
  • @Email - Checks if the format of the String is a valid Email
  • @NotBlank - Neither null, empty or contains only white spaces
  • @CreditcardNumber - Checks for user mistakes, not CC validity
  • @Length - Charactersequence has to be between min and max
  • @DecimalMax - Checks if value is smaller than the specific value in DecimalMax. It is also possible to set inclusive=true so the comparison will be less or equals.
  • @DecimalMin - Same like @DecimalMax, but the value has to be greater than the specific value in DecimalMin
  • @Digits - Checks if this is a number. Can contain also a specified amount of fractions
  • @Max - Checks if less than or equal to Max
  • @Min - Checks if higher than or equal to Min
  • @NotEmpty - Checks if not null or empty String or Collection
  • @Null - Value has to be null
  • @NotNull - Value shall not be null