Web/ReadingCode

[Spring Reading Code] ConstraintViolation

태애니 2023. 3. 28. 19:24
728x90

 

 

 

ConstraintViolation 는 Validation을 커스텀하게 사용할 수 있는 interface 이다

 

initiallize 에서는 

    ConstraintValidator.super.initialize(constraintAnnotation);

valid 할 검증 값들을 가져오고

 

 

isValid에서 검증할 내용을 작성한다

true 이면 검증에 통과되었다는 뜻이고

flase이면 에러가 나도록 한다

 

 

/**
 * Defines the logic to validate a given constraint {@code A}
 * for a given object type {@code T}.
 * <p>
 * Implementations must comply to the following restriction:
 * <ul>
 *     <li>{@code T} must resolve to a non parameterized type</li>
 *     <li>or generic parameters of {@code T} must be unbounded
 *     wildcard types</li>
 * </ul>
 * <p>
 * The annotation {@link SupportedValidationTarget} can be put on a
 * {@code ConstraintValidator} implementation to mark it as supporting
 * cross-parameter constraints. Check out {@link SupportedValidationTarget}
 * and {@link Constraint} for more information.
 *
 * @param <A> the annotation type handled by an implementation
 * @param <T> the target type supported by an implementation
 *
 * @author Emmanuel Bernard
 * @author Hardy Ferentschik
 */
public interface ConstraintValidator<A extends Annotation, T> {

	/**
	 * Initializes the validator in preparation for
	 * {@link #isValid(Object, ConstraintValidatorContext)} calls.
	 * The constraint annotation for a given constraint declaration
	 * is passed.
	 * <p>
	 * This method is guaranteed to be called before any use of this instance for
	 * validation.
	 * <p>
	 * The default implementation is a no-op.
	 *
	 * @param constraintAnnotation annotation instance for a given constraint declaration
	 */
	default void initialize(A constraintAnnotation) {
	}

	/**
	 * Implements the validation logic.
	 * The state of {@code value} must not be altered.
	 * <p>
	 * This method can be accessed concurrently, thread-safety must be ensured
	 * by the implementation.
	 *
	 * @param value object to validate
	 * @param context context in which the constraint is evaluated
	 *
	 * @return {@code false} if {@code value} does not pass the constraint
	 */
	boolean isValid(T value, ConstraintValidatorContext context);
}

 

 

 

 

728x90

'Web > ReadingCode' 카테고리의 다른 글

[Spring Reading Code] @PostMapping  (0) 2023.02.26
[Spring Reading Code] @ControllerAdvice  (0) 2023.02.24
[Spring Reading Code] ErrorController.java  (0) 2023.02.22