Class Validator


  • public class Validator
    extends Object
    This class is used to collect invalid builder settings, combining the invalid field names with text explaining why the field is invalid. Helper methods are provided to perform the most common checks (not null, not null or empty string, and equals value). If the check fails, then the given field name and appropriate reason are added to the errors list.

    If validation requires more sophisticated validation, requireTrue(Predicate, Object, String, String) and requireTrue(BiPredicate, Object, Object, String, String, String) are provided. The user provides a predicate which tests either a single field or two fields in combination. If predicate.test returns false, then the field or fields are marked as invalid. The BiPredicate method may be used to test two fields which are mutually dependent. This means that setting one field to a given value limits the value which may be assigned to the other. If this bi-predicate fails, then both fields are in error.

    If the provided requires methods are not sufficient (such as testing the mutual validity of three or more fields), then the user must use custom validation code and, if the validation fails, use addError(MultiKey2) to enter the error into the validation error list.

    Author:
    Charles W. Rapp
    • Field Detail

      • NEWLINE

        public static final String NEWLINE
        OS-specific end-of-line string for quick reference.
      • NOT_SET

        public static final String NOT_SET
        Standard reason for a required message field not being set.
        See Also:
        Constant Field Values
      • NAME_INDEX

        public static final int NAME_INDEX
        The field name is stored in index 0.
        See Also:
        Constant Field Values
      • MESSAGE_INDEX

        public static final int MESSAGE_INDEX
        The message explaining why the field is invalid is stored in index 1.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Validator

        public Validator()
        Creates a new validator instance containing no problems.
    • Method Detail

      • toString

        public String toString()
        Returns the error list as text which each error output as "<field name>: <message>" on a separate line. If there are no errors, then returns an empty string.
        Overrides:
        toString in class Object
        Returns:
        textual representation of error list.
      • isEmpty

        public boolean isEmpty()
        Returns true if there are no validation errors and false otherwise.
        Returns:
        true if there are no validation errors.
      • size

        public int size()
        Returns error count.
        Returns:
        error count.
      • errors

        public List<MultiKey2<String,​String>> errors()
        Returns an unmodifiable copy of the field errors list.
        Returns:
        field errors list.
      • addError

        public Validator addError​(MultiKey2<String,​String> error)
        Adds the given error to the validator error list. If error is null, then it is ignored and nothing is added to the errors list. This is done to allow this method to be used in a Validator method chain
        Parameters:
        error - invalid field error. Ignored if null.
        Returns:
        this Validator instance.
      • requireNotNull

        public Validator requireNotNull​(Object value,
                                        String fieldName)
        Checks if the given value is null and, if so, adds the appropriate error against the field name.
        Parameters:
        value - field value.
        fieldName - name of field containing value.
        Returns:
        this Validator instance.
        See Also:
        requireNotNullOrEmpty(String, String)
      • requireNotNullOrEmpty

        public Validator requireNotNullOrEmpty​(String value,
                                               String fieldName)
        Checks if the given String value is either null or empty and, if so, adds the appropriate error against the field name.
        Parameters:
        value - string field value.
        fieldName - name of field containing value.
        Returns:
        this Validator instance.
        See Also:
        requireNotNull(Object, String)
      • requireEquals

        public Validator requireEquals​(Object value,
                                       Object expected,
                                       String fieldName)
        Check if the field value equals the expected value and, if not, adds the appropriate error against the field name.
        Parameters:
        value - field value.
        expected - expected value.
        fieldName - name of field containing value.
        Returns:
        this Validator instance.
      • requireTrue

        public <V> Validator requireTrue​(Predicate<V> predicate,
                                         V value,
                                         String fieldName,
                                         String message)
        If predicate.test(value) returns false, then the given field name and message are added to the field errors list. This method allows for more sophisticated test beyond check for a null value or equality.

        Please note that predicate.test(value) should return true if value is acceptable. Do not write the predicate test method to check if value is not valid.

        Type Parameters:
        V - value type.
        Parameters:
        predicate - test method checks if value is acceptable.
        value - field value.
        fieldName - name of field containing value.
        message - error message if predicate.test(value) returns false.
        Returns:
        this Validator instance.
        See Also:
        requireTrue(BiPredicate, Object, Object, String, String, String), requireTrue(boolean, String, String)
      • requireTrue

        public <V1,​V2> Validator requireTrue​(BiPredicate<V1,​V2> predicate,
                                                   V1 value1,
                                                   V2 value2,
                                                   String fieldName1,
                                                   String fieldName2,
                                                   String message)
        If predicate.test(value) returns false, then the given field name and message are added to the field errors list. This method allows for tests which require checking two fields against each other when their individual validity is dependent on the fields being mutually consistent. That is, setting the first field to a given value limits what value may be set to the second field.

        Please note that predicate.test(value) should return true if value is acceptable. Do not write the predicate test method to check if value is not valid.

        Type Parameters:
        V1 - value1 type.
        V2 - value2 type.
        Parameters:
        predicate - test method checks if value1 and value2 are mutually acceptable.
        value1 - first field's value.
        value2 - second field's value.
        fieldName1 - first field's name.
        fieldName2 - second field's name.
        message - message applied to both fields if predicate.test(value1, value2) returns false.
        Returns:
        this Validator instance.
        See Also:
        requireTrue(Predicate, Object, String, String), requireTrue(boolean, String, String)
      • throwException

        public void throwException​(Class<?> tc)
        Throws a ValidationException if this validator contains any problems. Otherwise does nothing.
        Parameters:
        tc - validation exception applies to building this class.
        Throws:
        ValidationException - if this validator has any listed problems.
      • throwException

        public void throwException​(Class<?> tc,
                                   Throwable cause)
        Throws a ValidationException if this validator contains any problems. Otherwise does nothing.
        Parameters:
        tc - validation exception applies to building this class.
        cause - underlying exception causing this validation exception.
        Throws:
        ValidationException - if this validator has any listed problems.