I want my custom validator to perform a DB query. I get an SQLException if the query parameter exceeds the column size. So, why can't I simply compose constraints?
Code:
@ReportAsSingleViolation
@Size(max=10) // avoid exception? I wish...
@Constraint(validatedBy=MyObjectExistsValidator.class)
...
public @interface MyObjectExists {...}
Code:
public class MyObjectExistsValidator ...
public boolean isValid(String value, ConstraintValidatorContext context) {
return null != getEntityManager().find(MyObject.class, value); // simplified query for example
}
}
This is a matter of both DRY and performance. (In reality, I have another custom annotation instead of @Size, the query is more complex...) If I use @ReportAsSingleViolation, and one of the composed constraints fails first, then what possible good is it to invoke the custom validator anyway? (Looking at HV 4.1.0.Final - ConstraintTree.java line 123 -- it should simply "return".)