Java Bean Validation Annotation List

In this post, we will list out Java bean validation and Hibernate validator useful annotations.

We validate a Java bean with the standard framework — JSR 380, also known as Bean Validation 2.0.

Validating user input is a super common requirement in most applications. And the Java Bean Validation framework has become the de-facto standard for handling this kind of logic.

JSR 380 is a specification of the Java API for bean validation, part of Jakarta EE and JavaSE. This ensures that the properties of a bean meet specific criteria, using annotations such as @NotNull, @Min, and @Max.

Hibernate Validator is the reference implementation of the validation API.

Bean Validation Annotations List

Let’s go through all the annotations which you can use in bean classes.

Default Annotations

@Digits(integer=, fraction=) - Checks whether the annotated value is a number having up to integer digits and fraction fractional digits.

@Email - Checks whether the specified character sequence is a valid email address.

@Max(value=) - Checks whether the annotated value is less than or equal to the specified maximum.

@Min(value=) - Checks whether the annotated value is higher than or equal to the specified minimum

@NotBlank - Checks that the annotated character sequence is not null and the trimmed length is greater than 0.

@NotEmpty - Checks whether the annotated element is not null nor empty.

@Null - Checks that the annotated value is null

@NotNull - Checks that the annotated value is not null

@Pattern(regex=, flags=) - Checks if the annotated string matches the regular expression regex considering the given flag match

@Size(min=, max=) - Checks if the annotated element’s size is between min and max (inclusive)

@Negative - Checks if the element is strictly negative. Zero values are considered invalid.

@NegativeOrZero - Checks if the element is negative or zero.

@Future - Checks whether the annotated date is in the future.

@FutureOrPresent - Checks whether the annotated date is in the present or in the future.

@PastOrPresent - Checks whether the annotated date is in the past or in the present.

Hibernate validator specific annotations

In addition to the constraints defined by the Bean Validation API, Hibernate Validator provides several useful custom constraints which are listed below.

@CreditCardNumber( ignoreNonDigitCharacters=) - Checks that the annotated character sequence passes the Luhn checksum test. Note, this validation aims to check for user mistakes, not credit card validity!

@Currency(value=) - Checks that the currency unit of the annotated javax.money.MonetaryAmount is part of the specified currency units.

@EAN - Checks that the annotated character sequence is a valid EAN barcode. The default is EAN-13.

@ISBN - Checks that the annotated character sequence is a valid ISBN.

@Length(min=, max=) - Validates that the annotated character sequence is between min and max included.

@Range(min=, max=) - Checks whether the annotated value lies between (inclusive) the specified minimum and maximum.

@UniqueElements - Checks that the annotated collection only contains unique elements.

@URL - Checks if the annotated character sequence is a valid URL according to RFC2396.

References




Comments