public class Hbv extends Object implements Jooby.Module
Validator,HibernateValidatorConfiguration andParser
{
use(new Hbv());
get("/", req -> {
Validator validator = req.require(Validator.class);
Car car = req.params().to(Car.class);
Set<ConstraintViolation> violations = validator.validate(car);
if (violations.size() > 0) {
// handle errors
...
}
});
}
Request.params() or Request.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.
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.
ConstraintValidatorFactory is the extension point for customizing how constraint
validators are instantiated and released.
In Jooby, a ConstraintValidatorFactory is powered by Guice.
hibernate.validator will be add it automatically:
application.conf:
hibernate.validator.fail_fast = trueOr programmatically:
{
use(new Hbv().doWith(config -> {
config.failFast(true);
}));
}
| Constructor and Description |
|---|
Hbv()
Creates a new
Hbv module. |
Hbv(Class<?>... classes)
Creates a new
Hbv module. |
Hbv(Predicate<com.google.inject.TypeLiteral<?>> predicate)
Creates a new
Hbv module. |
| Modifier and Type | Method and Description |
|---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Hbv |
doWith(BiConsumer<org.hibernate.validator.HibernateValidatorConfiguration,com.typesafe.config.Config> configurer)
Setup a configurer callback.
|
Hbv |
doWith(Consumer<org.hibernate.validator.HibernateValidatorConfiguration> configurer)
Setup a configurer callback.
|
public Hbv(Predicate<com.google.inject.TypeLiteral<?>> predicate)
Hbv module.predicate - A predicate to test if a class require validation or not.public Hbv(Class<?>... classes)
Hbv module.classes - List of classes that require validation.public Hbv()
Hbv module. No automatic validation is applied.public Hbv doWith(Consumer<org.hibernate.validator.HibernateValidatorConfiguration> configurer)
configurer - Configurer callback.public Hbv doWith(BiConsumer<org.hibernate.validator.HibernateValidatorConfiguration,com.typesafe.config.Config> configurer)
configurer - Configurer callback.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure in interface Jooby.Modulepublic com.typesafe.config.Config config()
config in interface Jooby.ModuleCopyright © 2019. All rights reserved.