Bean Validation
Overview
Helidon supports Bean Validation via its integration with JAX-RS/Jersey. The Jakarta Bean Validation specification defines an API to validate Java beans. Bean Validation is supported in REST resource classes as well as in regular application beans.
If bean validation is required outside JAX-RS/Jersey use cases, it is also available in Helidon. It follows the standard Jakarta Bean Validation specification which defines an API to validate Java beans.
Maven Coordinates
To enable Bean Validation, add the following dependency to your project’s
pom.xml (see Managing Dependencies).
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
For general validation, please add to your pom.xml:
<dependency>
<groupId>io.helidon.microprofile.bean-validation</groupId>
<artifactId>helidon-microprofile-bean-validation</artifactId>
</dependency>
API
The specification defines a small set of built-in constraints. Their usage is encouraged both in regular constraint declarations and as composing constraints. Using this set of constraints will enhance portability of your constraints across constraint-consuming frameworks relying on the metadata API (such as client side validation frameworks or database schema generation frameworks).
Built-in annotations are annotated with an empty @Constraint annotation to
avoid any dependency between the specification API and a specific
implementation. Each Jakarta Bean Validation provider must recognize built-in
constraint annotations as valid constraint definitions and provide compliant
constraint implementations for each. The built-in constraint validation
implementation is having a lower priority than an XML mapping definition. In
other words ConstraintValidator implementations for built-in constraints can be
overridden by using the XML mapping (see Overriding constraint definitions in
XML).
All built-in constraints are in the jakarta.validation.constraints package.
Here is the list of constraints and their declaration.
| Annotation | Description |
|---|---|
@Null | The annotated element must be null. Accepts any type. |
@NotNull | The annotated element must not be null. Accepts any type. |
@AssertTrue | The annotated element must be true. Supported types are boolean and Boolean. Null elements are considered valid. |
@AssertFalse | The annotated element must be false. Supported types are boolean and Boolean. Null elements are considered valid. |
@Min | The annotated element must be a number whose value must be higher or equal to the specified minimum.
Supported types are:
double and float are not supported due to rounding errors (some providers might provide some approximative support).
Null elements are considered valid. |
@Max | The annotated element must be a number lower or equal to the specified maximum.
Supported types are:
double and float are not supported due to rounding errors (some providers might provide some approximative support).
Null elements are considered valid. |
@DecimalMin | The annotated element must be a number whose value must be higher or equal to the specified minimum.
Supported types are:
double and float are not supported due to rounding errors (some providers might provide some approximative support).
Null elements are considered valid. |
@DecimalMax | The annotated element must be a number lower or equal to the specified maximum.
Supported types are:
double and float are not supported due to rounding errors (some providers might provide some approximative support).
Null elements are considered valid. |
@Negative | The annotated element must be a strictly negative number (i.e. 0 is considered as an invalid value).
Supported types are:
Null elements are considered valid. |
@NegativeOrZero | The annotated element must be a negative number or 0.
Supported types are:
Null elements are considered valid. |
@Positive | The annotated element must be a strictly positive number (i.e. 0 is considered as an invalid value).
Supported types are:
Null elements are considered valid. |
@PositiveOrZero | The annotated element must be a positive number or 0.
Supported types are:
Null elements are considered valid. |
@Size | The annotated element size must be between the specified boundaries (included).
Supported types are:
Null elements are considered valid. |
@Digits | The annotated element must be a number within accepted range.
Supported types are:
Null elements are considered valid. |
@Past | The annotated element must be an instant, date or time in the past. Now is defined by the ClockProvider attached to the Validator or ValidatorFactory. The default clockProvider defines the current time according to the virtual machine, applying the current default time zone if needed.
Supported types are:
Null elements are considered valid. |
@PastOrPresent | The annotated element must be an instant, date or time in the past or in the present. Now is defined by the ClockProvider attached to the Validator or ValidatorFactory. The default clockProvider defines the current time according to the virtual machine, applying the current default time zone if needed.
The notion of present is defined relatively to the type on which the constraint is used. For instance, if the constraint is on a Year, present would mean the whole current year.
Supported types are:
Null elements are considered valid. |
@Future | The annotated element must be an instant, date or time in the future. Now is defined by the ClockProvider attached to the Validator or ValidatorFactory. The default clockProvider defines the current time according to the virtual machine, applying the current default time zone if needed.
Supported types are:
Null elements are considered valid. |
@FutureOrPresent | The annotated element must be an instant, date or time in the present or in the future. Now is defined by the ClockProvider attached to the Validator or ValidatorFactory. The default clockProvider defines the current time according to the virtual machine, applying the current default time zone if needed.
The notion of present here is defined relatively to the type on which the constraint is used. For instance, if the constraint is on a Year, present would mean the whole current year.
Supported types are:
Null elements are considered valid. |
@Pattern | The annotated CharSequence must match the specified regular expression. The regular expression follows the Java regular expression conventions see java.util.regex.Pattern. Accepts CharSequence.
Null elements are considered valid. |
@NotEmpty | The annotated element must not be null nor empty.
Supported types are:
|
@NotBlank | The annotated element must not be null and must contain at least one non-whitespace character. Accepts CharSequence. |
@Email | The string has to be a well-formed email address. Exact semantics of what makes up a valid email address are left to Jakarta Bean Validation providers. Accepts CharSequence.
Null elements are considered valid. |
Configuration
Bean Validation can be configured using META-INF/validation.xml.
For more information about configuring the validator factory in validation.xml, see Hibernate Validator Documentation.
Examples
- The following example shows a simple resource method annotated with
@POSTwhose parameter must be not null and valid. Validating a parameter in this case implies making sure that any constraint annotations in theGreetingclass are satisfied. The resource method shall never be called if the validation fails, with a 400 (Bad Request) status code returned instead.@Path("helloworld") public class HelloWorld { @POST @Consumes(MediaType.APPLICATION_JSON) public void post(@NotNull @Valid Greeting greeting) { // ... } } - The following example shows a simple application with one field declared as
not null using
@NotNullannotation:public class GreetingHolder { @NotNull private String greeting; }
If the bean contains a method parameter annotated with @Valid, and GreetingHolder with null_greeting is passed, then a _ValidationException will be thrown:@ApplicationScoped public class GreetingProvider { private GreetingHolder greetingHolder; void setGreeting(@Valid GreetingHolder greetingHolder) { this.greetingHolder = greetingHolder; } }beans.xmlis required to identify beans and for bean validation to work properly.
Examples are available in our official GitHub repository.
Additional Information
Helidon uses Hibernate Bean Validator for general bean validation.