Search code examples
javahibernate-validatorbean-validation

JSR 303 - Composition Constraint


How do I execute a ConstraintValidator within a ConstraintValidator?

I have a custom @NotEmpty and have a custom ConstraintValidator's for each type, and it works fine.

Now I want to create a class-level constraint that checks at least one of specified fields is not empty using those custom ConstraintValidator's I already have. The constraint part(@ClassNotEmpty) is done, but the problem is the ConstraintValidator implementation. how do I obtain a ConstraintValidator instance for a given constraint? i.e.

isValid(Object value, ConstraintValidatorContext constraintValidatorContext)  {
    String[] fieldValues = getFieldValues(value, this.classNotEmpty.fieldNames());

    for (String fieldValue : fieldValues) {
        <What do I put here?>.isValid(fieldValue, constraintValidatorContext);
    }
}

Is there a way to do this without pulling up the validation routine to a helper class?

BTW, I'm using Spring and Hibernate Validator.


Solution

  • After further research, it appears that there is no known solution to obtain a ConstraintValidator instance for a given constraint, portable or not. A helper class seems to be the only way to do this.