T
- the type of the value to validate@FunctionalInterface public interface Validator<T> extends BiFunction<T,ValueContext,ValidationResult>, Serializable
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"); };
ValidationResult
Modifier and Type | Method and Description |
---|---|
static <T> Validator<T> |
alwaysPass()
Returns a validator that passes any value.
|
ValidationResult |
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.
|
andThen
ValidationResult apply(T value, ValueContext context)
ValidationResult
instance
representing the outcome of the validation.apply
in interface BiFunction<T,ValueContext,ValidationResult>
value
- the input value to validatecontext
- the value context for validationstatic <T> Validator<T> alwaysPass()
T
- the value typestatic <T> Validator<T> from(SerializablePredicate<T> guard, String errorMessage)
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");
T
- the value typeguard
- the function used to validate, not nullerrorMessage
- the message returned if validation fails, not nullstatic <T> Validator<T> from(SerializablePredicate<T> guard, String errorMessage, ErrorLevel errorLevel)
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);
T
- the value typeguard
- the function used to validate, not nullerrorMessage
- the message returned if validation fails, not nullerrorLevel
- the error level for failures from this validator, not nullstatic <T> Validator<T> from(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider)
Result.ok()
; if it returns false or throws an exception,
Result.error()
is returned with the message from the provider.T
- the value typeguard
- the function used to validate, not nullerrorMessageProvider
- the provider to generate error messages, not nullstatic <T> Validator<T> from(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider, ErrorLevel errorLevel)
Result.ok()
; if it returns false or throws an exception,
Result.error()
is returned with the message from the provider.T
- the value typeguard
- the function used to validate, not nullerrorMessageProvider
- the provider to generate error messages, not nullerrorLevel
- the error level for failures from this validator, not nullCopyright © 2025. All rights reserved.