Skip to content

Learn how to use the Bean Validation API 2.0 by testing it!

License

Notifications You must be signed in to change notification settings

seahrh/bean-validation-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bean-validation-example

Build Status

The Bean Validation API allows code reuse of validators through annotations. Validators pass constraint violations to the application so that it can decide error handling. Despite its name, the Bean Validation API does not require the use of JavaBeans or Java EE.

Goals

Learn how to use the Bean Validation API 2.0

  • What are the features and limitations?
  • Add tests to verify behaviour not covered in the original examples from Hibernate Validator's reference guide1
  • Find out the minimal dependency set required

Prerequisites

  • JDK 8
  • Apache Maven

Maven Dependencies

Two dependencies required.

  1. Hibernate Validator - implementation of the Bean Validation API

    pom.xml

    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.13.Final</version>
    </dependency>
  2. Unified EL - implementation of the Unified Expression Language (JSR 341) for evaluating dynamic expressions in constraint violation messages. Not required if the application is running in a Java EE container.

    pom.xml

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.1-b09</version>
    </dependency>

Evaluation

My personal opinion of Hibernate Validator: TLDR; yes I will definitely use it in my next Java project!

Features

  1. Out of the box, constraints can be declared on the following via Java annotations
    1. Fields
    2. Properties i.e. getter methods, following the JavaBean standard
    3. Values in standard Java containers like java.util.Iterable, java.util.Map and java.util.Optional
      1. Also works on nested containers like Map<@NotNull Part, List<@NotNull Manufacturer>>
      2. Custom containers require additional work by writing a ValueExtractor
    4. Validating the entire class, useful for interactions among fields e.g. the seating capacity of a car and number of passengers
    5. Inheriting contraints from the superclass
    6. Cascading validation through the object graph (when an object holds a reference to another object)
      1. Also works for containers
      2. null is ignored during cascaded validation
      3. The validation engine ensures that no infinite loops occur during cascaded validation, for example if two objects hold references to each other.
  2. Validators can check the entire object or parts of it e.g. a single field
  3. List of built-in constraints
    1. Common business objects like @Email, @CreditCardNumber, @Currency
    2. Web: @URL, @SafeHtml
    3. Time: @PastOrPresent, @Future
  4. Write your own custom validator or compose a new one from existing constraints
    1. By default, constraint composition is logical AND. It is also possible to configure logical OR and ALL FALSE
  5. Group constraints to check in stages
    1. This mimics business processes/object lifecycles that require different validation logic in each stage
    2. e.g. driving off a new car at the showroom
      1. car must have passed vehicle inspection
      2. driver must have a license
  6. Set dynamic error messages
  7. Dynamic payload
    1. Pass a payload to the constraint, so as to set the control flow of the validation
      1. e.g. a custom constraint @CheckCase can have two different payloads @CheckCase(UPPER) or @CheckCase(LOWER)
    2. As part of ConstraintViolation
      1. Customise error messages e.g. suggest valid values
      2. Provide hints to the application on how to handle the error e.g. error severity
  8. For reproducible testing, define what is the time now for time-related validation (such as @Past or @Future constraints)
  9. By default, validation reports all the errors found instead of stopping at the first error. Optionally the validator can be configured to fail fast e.g. expensive validation of large object graphs

Limitations

  1. Do not recommend use of method constraints
    1. Requires an additional library that has a method interception facility, like Spring AOP
    2. Can only used for instance methods (static methods are not supported)

References

[1]: Hibernate Validator reference guide and source code

[2]: Baeldung examples of Bean Validation API

About

Learn how to use the Bean Validation API 2.0 by testing it!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages