hibernate validator

hibernate validator

Bean validation via Hibernate Validator.

exports

  • Validator,
  • HibernateValidatorConfiguration
  • Parser

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-hbv</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new Hbv());

  get("/", req -> {
   Validator validator = require(Validator.class);
   Car car = req.params().to(Car.class);
   Set<ConstraintViolation> violations = validator.validate(car);
   if (violations.size() > 0) {
     // handle errors
     ...
   }
  });
}

### in combination with JSON parser Jooby does not have a validation API or a validation API for HTTP bodies. Instead, this validator wraps existing body parsers as they simply return the first deserialized object. Consequently, this module must be imported before you import any parser! If you were to use the Jackson JSON parser module it would look like this:

{
  // must be imported before any body parser
  use(new Hbv());
  use(new Jackson());

  // routes go here
}

automatic validations of HTTP params and body

Previous example demonstrate how to manually validate a bean created via: req.params() or req.body(). The boilerplate code can be avoided if you explicitly tell the validation module which classes require validation.

The previous example can be rewritten as:

{
  use(new Hbv(Car.class));

  get("/", () -> {
   Car car = req.params().to(Car.class);
   // a valid car is here
   ...
  });
}

Here a Parser will do the boilerplate part and throws a ConstraintViolationException.

rendering a ConstraintViolationException

The default err handler will render the ConstraintViolationException without problem, but suppose we have a JavaScript client and want to display the errors in a friendly way.

{
  use(new Jackson()); // JSON renderer

  use(new Hbv(Car.class)); // Validate Car objects

  err((req, rsp, err) -> {
    Throwable cause = err.getCause();
    if (cause instanceof ConstraintViolationException) {
      Set<ConstraintViolation<?>> constraints = ((ConstraintViolationException) cause)
            .getConstraintViolations();

      Map<Path, String> errors = constraints.stream()
          .collect(
              Collectors.toMap(
                  ConstraintViolation::getPropertyPath,
                  ConstraintViolation::getMessage
                  )
          );
      rsp.send(errors);
    }
  });

  get("/", () -> {
   Car car = req.params().to(Car.class);
   // a valid car is here
   ...
  });
}

The call to rsp.send(errors); will be rendered by Jackson (or any other that applies) and will produces a more friendly response, here it will be a JavaScript object with the errors.

constraint validator factory

ConstraintValidatorFactory is the extension point for customizing how constraint validators are instantiated and released.

In Jooby, a ConstraintValidatorFactory is powered by Guice and java.io.Closeable constraint will be release it. See ConstraintValidatorFactory

configuration

Any property defined at hibernate.validator will be add it automatically:

application.conf

hibernate.validator.fail_fast = true

Or programmatically:

{
  use(new Hbv().doWith(config -> {
    config.failFast(true);
  }));
}