Interface Validator<V>

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

    public interface Validator<V>
    extends io.github.mmm.base.lang.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
    • Method Detail

      • 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.
      • isMandatory

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

        default Object getMin()
        Returns:
        the minimum allowed value. Typically of type <V> but this can not be guaranteed.
      • getMax

        default Object getMax()
        Returns:
        the maximum allowed value. Typically of type <V> but this can not be guaranteed.
      • 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.