class SingIn {
private String login;
private String password;
public SingIn(String login, String password) {
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
}
Login property could be an email(pattern ".+@.+") or a phone number(pattern "\+\d+").
Is it possible somehow with javax annotation to validate the login property?
Yes, it's possible by composing 2 @Pattern
annotations with logical OR
:
@ConstraintComposition(OR)
@Pattern(regexp = ".+@.+")
@Pattern(regexp = "\+\d+")
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface EmailOrPhone {
See also the topic Boolean composition of constraints in the Hibernate Validator documentation.