상황
- CustomValidator 구현 중 예외 발생
해결
- ConstraintValidator를 implement한 class의 isValid 메서드 수정
기존 코드
@Override
public boolean isValid(Integer givenNumber, ConstraintValidatorContext constraintValidatorContext) {
boolean isValid = Arrays.asList(range).contains(givenNumber);
if(!isValid) {
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate(message);
}
return isValid;
}
수정된 코드
@Override
public boolean isValid(Integer givenNumber, ConstraintValidatorContext constraintValidatorContext) {
boolean isValid = Arrays.asList(range).contains(givenNumber);
if(!isValid) {
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
}
return isValid;
}