Hi,
Are there scenarios in which it is recommended to implement a custom javax.validation.Validator rather than implementing custom constraints and annotating classes with them?
To be more precise, let's pretend we have a model (entity, DTO, etc.) that has a complex validation logic. Are there scenarios in which it is advised that rather than annotating our DTO with a custom constraint annotation such as given in the following listing:
Code:
@ComplexConstraint
public class MyComplexDto {
}
one should instead implement a custom Validator:
Code:
Validator validator = new MyCustomValidator(); // this can be obtained via injection, etc.
validator.validate(myComplexDto);
I always assumed that Validator is primarily meant to be implemented by implementations of the JSR 303 spec, rather than end users on a case by case basis.
What do you think?
Thanks in advance,
Behrang