I was looking into using boolean logic with my bean validation using Hibernate Validator in scenarios where AND'ing the constraints does not suffice. I found that it is possible to change this default behavior by creating a new annotation with the @ConstraintComposition
annotation as described in the documentation. The documentation provides the following example.
@ConstraintComposition(OR)
@Pattern(regexp = "[a-z]")
@Size(min = 2, max = 3)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
String message() default "{org.hibernate.validator.referenceguide.chapter11." +
"booleancomposition.PatternOrSize.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Using this @PatternOrSize
validation constraint means that the input string is either lowercased or has a size between 2 and 3. Now this raises a couple of questions:
- I believe one has to create a new annotation to change the default boolean logic behavior. Is this correct? If one could define the boolean logic at a class field, that would be really nice, but it may not be possible?
- Is it possible to further customize the boolean logic behavior without creating a custom validator, e.g. defining
AND
andOR
at the same time? I could not find anything that suggests that this is possible, but maybe I missed something. - And most importantly: is it possible to make the arguments to the
@Pattern
and@Size
constraints dynamic? Because otherwise I guess I would have to define a new annotation for every combination of arguments that I need. Defining a new annotation simply to change the arguments to@Size
does not seem feasible, but may be necessary?
Thank you in advance.