Package com.vaadin.flow.data.binder
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.
@FunctionalInterface
public interface Validator<T>
extends BiFunction<T,ValueContext,ValidationResult>, Serializable
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 Summary
Modifier and TypeMethodDescriptionstatic <T> Validator<T>
Returns a validator that passes any value.apply
(T value, ValueContext context) Validates the given value.static <T> Validator<T>
from
(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider) Builds a validator out of a conditional function and an error message provider.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.static <T> Validator<T>
from
(SerializablePredicate<T> guard, String errorMessage) Builds a validator out of a conditional function and an error message.static <T> Validator<T>
from
(SerializablePredicate<T> guard, String errorMessage, ErrorLevel errorLevel) Builds a validator out of a conditional function and an error message.Methods inherited from interface java.util.function.BiFunction
andThen
-
Method Details
-
apply
Validates the given value. Returns aValidationResult
instance representing the outcome of the validation.- Specified by:
apply
in interfaceBiFunction<T,
ValueContext, ValidationResult> - Parameters:
value
- the input value to validatecontext
- the value context for validation- Returns:
- the validation result
-
alwaysPass
Returns a validator that passes any value.- Type Parameters:
T
- the value type- Returns:
- an always-passing validator
-
from
Builds a validator out of a conditional function and an error message. If the function returns true, the validator returnsResult.ok()
; if it returns false or throws an exception,ValidationResult.error(String)
is returned with the given message and error levelErrorLevel.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 nullerrorMessage
- 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 returnsResult.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 nullerrorMessage
- the message returned if validation fails, not nullerrorLevel
- 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 returnsResult.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 nullerrorMessageProvider
- 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 returnsResult.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 nullerrorMessageProvider
- the provider to generate error messages, not nullerrorLevel
- the error level for failures from this validator, not null- Returns:
- the new validator using the function
-