spring combine two validation annotations in one
You can add
@ConstraintComposition
Who is by default at AND and if one day you want @NotNull or @Size You add
@ConstraintComposition(OR)
Edit : this is an exemple who combine @Pattern(regexp = "[a-z]")
and
@Size(min = 2, max = 3)
@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 "";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
This is a bit late, but technique of combining validation annotations described in
https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=5.4#section-constraint-composition
Maybe it was not available, at the time of writing, but solution is following
@NotNull
@Size(min=4)
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
@Documented
public @interface JPasswordConstraint {
String message() default "Password is invalid";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}