Bean validation via Hibernate Validator.
1) Add the dependency:
.
2) Install
import io.jooby.hibernate.validator.HibernateValidatorModule;
{
install(new HibernateValidatorModule());
}import io.jooby.hibernate.validator.HibernateValidatorModule
{
install(HibernateValidatorModule())
}3) Usage in MVC routes
import io.jooby.annotation.*;
import jakarta.validation.Valid;
@Path("/mvc")
public class Controller {
@POST("/validate-body")
public void validateBody(@Valid Bean bean) { // (1)
...
}
@POST("/validate-query")
public void validateQuery(@Valid @QueryParam Bean bean) { // (2)
...
}
@POST("/validate-list")
public void validateList(@Valid List<Bean> beans) { // (3)
...
}
@POST("/validate-map")
public void validateMap(@Valid Map<String, Bean> beans) { // (4)
...
}
}import io.jooby.annotation.*;
import jakarta.validation.Valid
@Path("/mvc")
class Controller {
@POST("/validate-body")
fun validateBody(@Valid bean: Bean) : Unit { // (1)
...
}
@POST("/validate-query")
fun validateQuery(@Valid @QueryParam bean: Bean) : Unit { // (2)
...
}
@POST("/validate-list")
fun validateList(@Valid beans: List<Bean>) : Unit { // (3)
...
}
@POST("/validate-map")
fun validateMap(@Valid beans: Map<String, Bean>) : Unit { // (4)
...
}
}-
Validate a bean decoded from the request body
-
Validate a bean parsed from query parameters. This works the same for
@FormParamor@BindParam -
Validate a list of beans. This also applies to arrays
@Valid Bean[] beans -
Validate a map of beans
4) Usage in in script/lambda routes
import io.jooby.validation.BeanValidator;
{
use(BeanValidator.validate());
post("/validate", ctx -> {
Bean bean = ctx.body(Bean.class);
...
});
}import io.jooby.validation.BeanValidator
{
use(BeanValidator.validate())
post("/validate") {
val bean = ctx, ctx.body(Bean.class)
...
}
}BeanValidator.validate() behaves identically to validation in MVC routes.
It also supports validating list, array, and map of beans
There is a handler version of it, so you can apply per route:
import io.jooby.validation.BeanValidator.validate;
{
post("/validate", validate(ctx -> {
Bean bean = ctx.body(Bean.class);
...
}));
}HibernateValidatorModule provides default built-in error handler that
catches ConstraintViolationException and transforms it into the following response:
{
"title": "Validation failed",
"status": 422,
"errors": [
{
"field": "firstName",
"messages": [
"must not be empty",
"must not be null"
],
"type": "FIELD"
},
{
"field": null,
"messages": [
"passwords are not the same"
],
"type": "GLOBAL"
}
]
}|
Note
|
|
It is possible to override the title and status code of the response above:
{
install(new Jackson2Module());
install(new HibernateValidatorModule()
.statusCode(StatusCode.BAD_REQUEST)
.validationTitle("Incorrect input data")
);
}If the default error handler doesn’t fully meet your needs, you can always disable it and provide your own:
{
install(new Jackson2Module());
install(new HibernateValidatorModule().disableViolationHandler());
error(ConstraintViolationException.class, new MyConstraintViolationHandler());
}The module exposes Validator as a service, allowing you to run validation manually at any time.
import jakarta.validation.Validator;
{
post("/validate", ctx -> {
var validator = require(Validator.class);
var violations = validator.validate(ctx.body(Bean.class));
if (!violations.isEmpty()) {
...
}
...
});
}1) Install DI framework at first.
import io.jooby.hibernate.validator.HibernateValidatorModule;
{
install(new GuiceModule()); // (1)
install(new HibernateValidatorModule());
}-
Guiceis just an example, you can achieve the same withAvajeorDagger
2) Inject Validator in controller, service etc.
import jakarta.validation.Validator;
import jakarta.inject.Inject;
@Path("/mvc")
public class Controller {
private final Validator validator;
@Inject
public Controller(Validator validator) {
this.validator = validator;
}
@POST("/validate")
public void validate(Bean bean) {
Set<ConstraintViolation<Bean>> violations = validator.validate(bean);
...
}
}As you know, Hibernate Validator allows you to build fully custom ConstraintValidator.
In some scenarios, you may need access not only to the bean but also to services, repositories, or other resources
to perform more complex validations required by business rules.
In this case you need to implement a custom ConstraintValidator that will rely on your DI framework.
@Constraint(validatedBy = MyCustomValidator.class)
@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface MyCustomAnnotation {
String message() default "My custom message";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class MyCustomValidator implements ConstraintValidator<MyCustomAnnotation, Bean> {
// This is the service you want to inject
private final MyService myService;
@Inject
public MyCustomValidator(MyService myService) {
this.myService = myService;
}
@Override
public boolean isValid(Bean bean, ConstraintValidatorContext context) {
// Use the injected service for validation logic
return myService.isValid(bean);
}
}Any property defined at hibernate.validator will be added automatically:
hibernate.validator.fail_fast = trueOr programmatically:
import io.jooby.hibernate.validator.HibernateValidatorModule;
{
var cfg = byProvider(HibernateValidator.class).configure();
cfg.failFast(true);
install(new HibernateValidatorModule(cfg));
}Just install HibernateValidatorModule before HibernateModule, like:
import io.jooby.hibernate.validator.HibernateValidatorModule;
{
install(new HibernateValidatorModule());
install(new HibernateModule());
}The HibernateModule will detect the constraint validator factory and setup. This avoid creating
a new instance of constraint validator factory.