I was attempted to apply multiple @Pattern annotations to a single field:
@Pattern(regexp = \"(?=.*[0-9])\", message = \"Password must contain one digit.\")
I modified Gunnar answer and write composite constraint and this now seems to work correctly on 4 unit tests:
@NotNull
@Size(min=6, max=45)
@Pattern.List({
@Pattern(regexp = "(?=.*[0-9]).+", message = "Password must contain one digit."),
@Pattern(regexp = "(?=.*[a-z]).+", message = "Password must contain one lowercase letter."),
@Pattern(regexp = "(?=.*[A-Z]).+", message = "Password must contain one uppercase letter."),
@Pattern(regexp = "(?=.*[!@#$%^&*+=?-_()/\"\\.,<>~`;:]).+", message ="Password must contain one special character."),
@Pattern(regexp = "(?=\\S+$).+", message = "Password must contain no whitespace.")
})
@Constraint(validatedBy = {}) // constraints composition
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface StrongPassword {
String message() default "Password doesn't match bean validation constraints.";
Class>[] groups() default {};
Class extends Payload>[] payload() default {};
}