Type Parameters:
S - the type of validator
T - the type of the value that is being validated
All Known Subinterfaces:
BigDecimalValidator, BigIntegerValidator, BooleanValidator, ByteArrayValidator, ByteValidator, CharacterValidator, CollectionValidator<T,E>, ComparableValidator<T>, DoubleValidator, FloatValidator, GenericTypeValidator<T>, InetAddressValidator, IntegerValidator, JsonNodeValidator<T>, ListValidator<T,E>, LongValidator, MapValidator<T,K,V>, MultimapValidator<T,K,V>, ObjectArrayValidator<T,E>, ObjectValidator<T>, OptionalValidator<T>, PathValidator, PrimitiveBooleanArrayValidator, PrimitiveBooleanValidator, PrimitiveByteArrayValidator, PrimitiveByteValidator, PrimitiveCharacterArrayValidator, PrimitiveCharacterValidator, PrimitiveDoubleArrayValidator, PrimitiveDoubleValidator, PrimitiveFloatArrayValidator, PrimitiveFloatValidator, PrimitiveIntegerArrayValidator, PrimitiveIntegerValidator, PrimitiveLongArrayValidator, PrimitiveLongValidator, PrimitiveShortArrayValidator, PrimitiveShortValidator, PrimitiveUnsignedIntegerValidator, ShortValidator, StringValidator, UnsignedIntegerValidator, UriValidator

public interface ValidatorComponent<S,T>
Methods that all validators must contain.
  • Method Details

    • getName

      String getName()
      Returns the name of the value.
      Returns:
      the name of the value
    • getValueOrDefault

      T getValueOrDefault(T defaultValue)
      Returns the value that is being validated.
      Parameters:
      defaultValue - the fallback value to use if the value is invalid
      Returns:
      the value, or defaultValue if the value is invalid (e.g. due to dereferencing a property of a null object)
    • getContext

      Map<String,Optional<Object>> getContext()
      Returns the contextual information for upcoming validations carried out by this validator. The contextual information is a map of key-value pairs that can provide more details about validation failures. For example, if the message is "Password may not be empty" and the map contains the key-value pair {"username": "john.smith"}, the exception message would be:

      Password may not be empty
      username: john.smith

      Note that values are wrapped in an Optional because modern maps do not support null values.

      Returns:
      an unmodifiable map from each entry's name to its value
      See Also:
    • withContext

      S withContext(Object value, String name)
      Sets the contextual information for upcoming validations.

      This method adds contextual information to exception messages. The contextual information is stored as key-value pairs in a map. Values set by this method override any values that are set using Validators.withContext(Object, String).

      There is no way to remove contextual information from a validator. Thread-level contextual information is removed automatically.

      Parameters:
      value - the value of the entry
      name - the name of an entry
      Returns:
      this
      Throws:
      NullPointerException - if name is null
      IllegalArgumentException - if name:
      • contains whitespace
      • is empty
      • is already in use by the value being validated or the validator context
    • and

      S and(Consumer<? super S> validation)
      Facilitates the validation of related properties. For example:

       requireThat(nameToFrequency, "nameToFrequency").
         and(m -> m.size().isPositive()).
         and(m -> m.keySet().contains("John"));
      

      Any changes made during the validation process will impact this validator.

      Parameters:
      validation - the nested validation
      Returns:
      this
      Throws:
      NullPointerException - if validation is null
    • validationFailed

      boolean validationFailed()
      Checks if any validation has failed.
      Returns:
      true if at least one validation has failed
    • elseGetFailures

      ValidationFailures elseGetFailures()
      Returns the list of failed validations.
      Returns:
      an unmodifiable list of failed validations
    • elseThrow

      boolean elseThrow()
      Throws an exception if a validation failed; otherwise, returns true.
      Returns:
      true if the validation passed
      Throws:
      RuntimeException - if a method precondition was violated
      Error - if a class invariant or method postcondition was violated
      MultipleFailuresException - if more than one validation failed. This exception contains a list of the failures.
    • getContextAsString

      String getContextAsString()
      Returns the contextual information associated with this validator.
      Returns:
      the contextual information associated with this validator