When validating some configuration I'm loading from application.yml using custom validator like the following:
public class MyValidator implements ConstraintValidator<MyAnnotation, List<MyClass>> {
@Override
public void initialize(MyAnnotation myAnnotation) {
//nothing to do
}
@Override
public boolean isValid(List<MyClass> myList, ConstraintValidatorContext context) {
// validation logic
}
}
if you validation logic triggers an exception, e.g in case of null pointing, I noticed that other possible validators do not kick in and an error message, that reads something like binding error, is displayed.
what's the best practice to avoid this situation ?
So the general best practice with Hibernate Validator's validators is that they should consider a null entry as valid.
If you check the ones we provide in Hibernate Validator, they all return true for a null entry. The idea is to explicitly use @NotNull
when you want to check for null.
There are a few exceptions such as @NotBlank
but this is the general best practice.
Obviously, as said above, your code needs to be correct and avoid NPE or other exceptions. But that goes without saying.