001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.validation;
018
019/**
020 * A validator for application-specific objects.
021 *
022 * <p>This interface is totally divorced from any infrastructure
023 * or context; that is to say it is not coupled to validating
024 * only objects in the web tier, the data-access tier, or the
025 * whatever-tier. As such it is amenable to being used in any layer
026 * of an application, and supports the encapsulation of validation
027 * logic as a first-class citizen in its own right.
028 *
029 * <p>Find below a simple but complete {@code Validator}
030 * implementation, which validates that the various {@link String}
031 * properties of a {@code UserLogin} instance are not empty
032 * (that is they are not {@code null} and do not consist
033 * wholly of whitespace), and that any password that is present is
034 * at least {@code 'MINIMUM_PASSWORD_LENGTH'} characters in length.
035 *
036 * <pre class="code"> public class UserLoginValidator implements Validator {
037 *
038 *    private static final int MINIMUM_PASSWORD_LENGTH = 6;
039 *
040 *    public boolean supports(Class clazz) {
041 *       return UserLogin.class.isAssignableFrom(clazz);
042 *    }
043 *
044 *    public void validate(Object target, Errors errors) {
045 *       ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "field.required");
046 *       ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
047 *       UserLogin login = (UserLogin) target;
048 *       if (login.getPassword() != null
049 *             && login.getPassword().trim().length() < MINIMUM_PASSWORD_LENGTH) {
050 *          errors.rejectValue("password", "field.min.length",
051 *                new Object[]{Integer.valueOf(MINIMUM_PASSWORD_LENGTH)},
052 *                "The password must be at least [" + MINIMUM_PASSWORD_LENGTH + "] characters in length.");
053 *       }
054 *    }
055 * }</pre>
056 *
057 * <p>See also the Spring reference manual for a fuller discussion of
058 * the {@code Validator} interface and its role in an enterprise
059 * application.
060 *
061 * @author Rod Johnson
062 * @see SmartValidator
063 * @see Errors
064 * @see ValidationUtils
065 */
066public interface Validator {
067
068        /**
069         * Can this {@link Validator} {@link #validate(Object, Errors) validate}
070         * instances of the supplied {@code clazz}?
071         * <p>This method is <i>typically</i> implemented like so:
072         * <pre class="code">return Foo.class.isAssignableFrom(clazz);</pre>
073         * (Where {@code Foo} is the class (or superclass) of the actual
074         * object instance that is to be {@link #validate(Object, Errors) validated}.)
075         * @param clazz the {@link Class} that this {@link Validator} is
076         * being asked if it can {@link #validate(Object, Errors) validate}
077         * @return {@code true} if this {@link Validator} can indeed
078         * {@link #validate(Object, Errors) validate} instances of the
079         * supplied {@code clazz}
080         */
081        boolean supports(Class<?> clazz);
082
083        /**
084         * Validate the supplied {@code target} object, which must be
085         * of a {@link Class} for which the {@link #supports(Class)} method
086         * typically has (or would) return {@code true}.
087         * <p>The supplied {@link Errors errors} instance can be used to report
088         * any resulting validation errors.
089         * @param target the object that is to be validated
090         * @param errors contextual state about the validation process
091         * @see ValidationUtils
092         */
093        void validate(Object target, Errors errors);
094
095}