-
Notifications
You must be signed in to change notification settings - Fork 92
Bean Validation
Achilles allows you to apply Bean Validation (JSR-303) to entities before save or update action.
To activate bean validation, all you need to do is:
- set achilles.bean.validation.enable to
true
- annotate your entities with proper validation annotations
- add a bean validation implementation as dependency of your project. You can use the reference implementation (Hibernate Validator)
A special Interceptor is built when bean validation is enabled. This interceptor will be the last interceptor to be triggered if more interceptors are registered for an entity.
This way, you can perform functional logic on the entity before invoking bean validation. The interceptor is bound to the PRE_PERSIST
and PRE_UPDATE
events of the entity lifecycle.
Achilles will apply validation on entity having constraints on field, property or class (custom validation). Right now Achilles does not validate method arguments or return values.
On validation error, an AchillesBeanValidationException
will be thrown. You can setup a try-catch block around this exception for validation error handling.
## Validation group
The default bean validation interceptor will not be invoked for grouped constraints. If you want to use them, you can provide a custom bean validation interceptor. Below is an example of such interceptor:
public class Entity {
@Id
private Long id;
@Column
@NotNull(groups = CustomValidationGroup.class)
private String name;
}
public static class CustomValidationInterceptor implements Interceptor<Entity> {
private final Validator validator;
public CustomValidationInterceptor(Validator validator) {
this.validator = validator;
}
@Override
public void onEvent(Entity entity) {
Set<ConstraintViolation<Entity>> violations = validator.validate(entity,CustomValidationGroup.class);
...
}
@Override
public List<Event> events() {
return asList(PRE_PERSIST, PRE_UPDATE);
}
}
## Extending validation to others lifecycle events
It is possible to extend bean validation to other lifecycle events than PRE_PERSIST
and PRE_UPDATE
. You just need to provide custom bean validation interceptors. Follow the same approach as above
-
Bootstraping Achilles at runtime
- Runtime Configuration Parameters
-
Manager
-
Consistency Level
-
Cassandra Options at runtime
-
Lightweight Transaction (LWT)
-
JSON Serialization
-
Interceptors
-
Bean Validation (JSR-303)