Interface Validator<V>

Type Parameters:
V - type of the value to validate.
All Superinterfaces:
Composable<Validator<?>>, Iterable<Validator<?>>
All Known Implementing Classes:
AbstractComposedValidator, AbstractValidator, AbstractValueValidator, ComposedValidator, ProjectionValidator

public interface Validator<V> extends Composable<Validator<?>>
A Validator allows to validate according values.
There can be arbitrary implementations of this interface. A regular implementation shall be stateless and therefore thread-safe. All parameterization shall therefore happen on initialization - ideally at construction.
NOTE:
This API intentionally does NOT make use of exceptions as they are more expensive to produce and shall only occur in exceptional situations, while a validation failure is a regular use-case. Further, a validation shall validate entire objects to the end collecting all failures so the end-user can see and fix all problems at once.
ATTENTION:
The null values is typically only handled by mandatory validator. Other validators will treat null as a valid value. This design gives the best flexibility as it allows to define specific constraints also for optional values. However, you need to be aware this fact to avoid mistakes. So e.g. adding a validator requiring that the minimum size/length of a value needs to be e.g. 2 will still accept null as valid. So in such case you most probably want to combine it with the mandatory validator.
Since:
1.0.0
  • Field Details

  • Method Details

    • validate

      default ValidationResult validate(V value)
      This method validates the given value.
      Parameters:
      value - is the value to validate.
      Returns:
      the ValidationResult or null if the given value is valid according to this Validator.
    • validate

      ValidationResult validate(V value, Object valueSource)
      This method validates the given value.
      Parameters:
      value - is the value to validate.
      valueSource - is the source describing the origin of the given value. The source needs to have a reasonable string-representation as this may be displayed to the end-user to locate the source of the failure. In most cases it is suitable to directly pass a String.
      Returns:
      the ValidationResult.
    • getId

      String getId()
      Returns:
      the identifier of this Validator.
      See Also:
    • isMandatory

      default boolean isMandatory()
      Returns:
      true if this is a validator for mandatory fields (that will not accept null or empty values), false otherwise.
    • containsId

      default boolean containsId(String id)
      Parameters:
      id - the ID of the Validator to check for.
      Returns:
      true if this Validator itself has the given ID or recursively contains such Validator, false otherwise.
    • getChild

      default <T> T getChild(Class<T> validatorClass)
      Type Parameters:
      T - type of the requested Validator.
      Parameters:
      validatorClass - the Class reflecting the requested Validator.
      Returns:
      the requested Validator or null if this validator is not of the given type and does not contain any child (recursively) of the given type.
    • getRange

      default <T extends Comparable<?>> Range<T> getRange()
      Type Parameters:
      T - type of the Range value. Typically of type <V> but e.g. for String or Collection the type would be Integer to validate the size of the actual value.
      Returns:
      the Range constraint with an optional mimimum and/or maximum value. Will be Range.unbounded() by if no bounds are validated.
    • getMin

      default <T extends Comparable<?>> T getMin()
      Type Parameters:
      T - type of the minimum value. Typically of type <V> but e.g. for String or Collection the type would be Integer to validate the size of the actual value.
      Returns:
      the minimum allowed value.
    • getMax

      default <T extends Comparable<?>> T getMax()
      Type Parameters:
      T - type of the minimum value. Typically of type <V> but e.g. for String or Collection the type would be Integer to validate the size of the actual value.
      Returns:
      the maximum allowed value.
    • append

      default <T> Validator<T> append(Validator<?> validator)
      Type Parameters:
      T - type of the value to validate.
      Parameters:
      validator - the Validator to append.
      Returns:
      a new Validator instance composing this Validator with the given Validator.
    • append

      default <T> Validator<T> append(Validator<?>... validators)
      Type Parameters:
      T - type of the value to validate.
      Parameters:
      validators - the Validators to append.
      Returns:
      a new Validator instance composing this Validator with the given Validators.
    • none

      static <T> Validator<T> none()
      Type Parameters:
      T - type of the value to validate.
      Returns:
      an instance of Validator that always return ValidationResultValid (accepts any value as valid input).
    • isValidating

      static boolean isValidating(Validator<?> validator)
      Parameters:
      validator - the Validator to check. May be null.
      Returns:
      true if the given Validator is neither null nor none, false otherwise.