Interface Validator<T>

Type Parameters:
T - the type of the value to validate
All Superinterfaces:
BiFunction<T,ValueContext,ValidationResult>, Serializable
All Known Implementing Classes:
AbstractValidator, BeanValidator, BigDecimalRangeValidator, BigIntegerRangeValidator, ByteRangeValidator, DateRangeValidator, DateTimeRangeValidator, DoubleRangeValidator, EmailValidator, FloatRangeValidator, IntegerRangeValidator, LongRangeValidator, RangeValidator, RegexpValidator, ShortRangeValidator, StringLengthValidator
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

A functional interface for validating user input or other potentially invalid data. When a validator instance is applied to a value of the corresponding type, it returns a result signifying that the value either passed or failed the validation.

For instance, the following validator checks if a number is positive:

 Validator<Integer> v = num -> {
     if (num >= 0)
         return ValidationResult.ok();
     else
         return ValidationResult.error("number must be positive");
 };
 
Since:
1.0.
Author:
Vaadin Ltd
See Also:
  • Method Details

    • apply

      ValidationResult apply(T value, ValueContext context)
      Validates the given value. Returns a ValidationResult instance representing the outcome of the validation.
      Specified by:
      apply in interface BiFunction<T,ValueContext,ValidationResult>
      Parameters:
      value - the input value to validate
      context - the value context for validation
      Returns:
      the validation result
    • alwaysPass

      static <T> Validator<T> alwaysPass()
      Returns a validator that passes any value.
      Type Parameters:
      T - the value type
      Returns:
      an always-passing validator
    • from

      static <T> Validator<T> from(SerializablePredicate<T> guard, String errorMessage)
      Builds a validator out of a conditional function and an error message. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, ValidationResult.error(String) is returned with the given message and error level ErrorLevel.ERROR.

      For instance, the following validator checks if a number is between 0 and 10, inclusive:

       Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10,
               "number must be between 0 and 10");
       
      Type Parameters:
      T - the value type
      Parameters:
      guard - the function used to validate, not null
      errorMessage - the message returned if validation fails, not null
      Returns:
      the new validator using the function
    • from

      static <T> Validator<T> from(SerializablePredicate<T> guard, String errorMessage, ErrorLevel errorLevel)
      Builds a validator out of a conditional function and an error message. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, ValidationResult.error(String) is returned with the given message and error level.

      For instance, the following validator checks if a number is between 0 and 10, inclusive:

       Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10,
               "number must be between 0 and 10", ErrorLevel.ERROR);
       
      Type Parameters:
      T - the value type
      Parameters:
      guard - the function used to validate, not null
      errorMessage - the message returned if validation fails, not null
      errorLevel - the error level for failures from this validator, not null
      Returns:
      the new validator using the function
    • from

      static <T> Validator<T> from(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider)
      Builds a validator out of a conditional function and an error message provider. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, Result.error() is returned with the message from the provider.
      Type Parameters:
      T - the value type
      Parameters:
      guard - the function used to validate, not null
      errorMessageProvider - the provider to generate error messages, not null
      Returns:
      the new validator using the function
    • from

      static <T> Validator<T> from(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider, ErrorLevel errorLevel)
      Builds a validator out of a conditional function and an error message provider. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, Result.error() is returned with the message from the provider.
      Type Parameters:
      T - the value type
      Parameters:
      guard - the function used to validate, not null
      errorMessageProvider - the provider to generate error messages, not null
      errorLevel - the error level for failures from this validator, not null
      Returns:
      the new validator using the function