Is it possible to get the groups within the validator the validation method is invoked with?
I have multiple groups (Create, Update, Delete) which most result in similar validation for the one bean.
Instead of providing multiple almost identical validators (and create utility functions to externalize same validation code) I would prefer to have a single Validator which handles the validation regarding the groups the validation was invoked with.
In worst case I have 3 times n
single Validators and n
utility classes for shared code instead of just n validators.
Validator.validate(Object, Class<?> ... groups)
How do I get those groups within my Validator to do something pseudo-like?
if (groups.contains(Create.class)) // validate create stuff
In case you are asking how to determine the currently validated constraints within a custom constrain (ConstraintValidator implementation), the answer is you cannot.
The concepts of groups and constraint are orthogonal. A constraint should not behave differently depending on the group which gets validated.
In this context, think about users of constraints. How would they know what their constraint does if the validation is conditional?