001/*
002 * Copyright 2002-2012 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 Errors
063 * @see ValidationUtils
064 */
065public interface Validator {
066
067        /**
068         * Can this {@link Validator} {@link #validate(Object, Errors) validate}
069         * instances of the supplied {@code clazz}?
070         * <p>This method is <i>typically</i> implemented like so:
071         * <pre class="code">return Foo.class.isAssignableFrom(clazz);</pre>
072         * (Where {@code Foo} is the class (or superclass) of the actual
073         * object instance that is to be {@link #validate(Object, Errors) validated}.)
074         * @param clazz the {@link Class} that this {@link Validator} is
075         * being asked if it can {@link #validate(Object, Errors) validate}
076         * @return {@code true} if this {@link Validator} can indeed
077         * {@link #validate(Object, Errors) validate} instances of the
078         * supplied {@code clazz}
079         */
080        boolean supports(Class<?> clazz);
081
082        /**
083         * Validate the supplied {@code target} object, which must be
084         * of a {@link Class} for which the {@link #supports(Class)} method
085         * typically has (or would) return {@code true}.
086         * <p>The supplied {@link Errors errors} instance can be used to report
087         * any resulting validation errors.
088         * @param target the object that is to be validated (can be {@code null})
089         * @param errors contextual state about the validation process (never {@code null})
090         * @see ValidationUtils
091         */
092        void validate(Object target, Errors errors);
093
094}