I use the javax.validation and have a Hibernate validator on a classpath.
I would like to validate entities of a specific class on the class level (not fields level).
I can do this with by creating a custom class-level annotation and a paired ConstraintValidator as shown below:
@Constraint(validatedBy = CustomValidator.class)
@Target({ ElementType.TYPE })
public @interface CustomConstraintAnnotation {
}
class CustomValidator
implements ConstraintValidator<CustomConstraintAnnotation, Object> {
}
However, I would like to avoid creating a custom annotation and assign a custom validator per entity type, so that the validator would be registered for this type and validate instances of this type by default when validator.validate(entity) is invoked without having to annotate the type.
The javax.validation.ConstraintValidator does not fit, because it works only in pair with an annotation.
Basically, I would like to create a mapping between the type and the corresponding custom validator without using an annotation.
Is there a way to achieve this?