Say I have a request payload class like the following:
@Data
class Payload {
Type type;
@Valid
Details details;
}
And,
@Data
class Dtails {
@OnlyAlphabet
String name;
String email;
}
I have the enum defined as follows:
public enum Type {
HUMAN,
ALIEN
}
I have defined the @OnlyAlphabet constraint like this:
@Target({TYPE, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = NameValidator.class)
public @interface OnlyAlphabet {
String message() default "Invalid Name";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
And,
public class NameValidator implements ConstraintValidator<OnlyAlphabet, String> {
@Override
public void initialize(OnlyAlphabet constraintAnnotation) {
}
@Override
public boolean isValid(String name, ConstraintValidatorContext constraintValidatorContext) {
return isContainOnlyAlphabet(name);
}
}
If the type is ALIEN, I want the @OnlyAlphabet validation constraint disabled. Or even, based on the type property, I want to use another annotation. Is there any way I can achieve it?