Interface Validator<T>
- Type Parameters:
T
- the type of the value being validated
- 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 representing a strategy for handling invalid values
detected during validation.
Implementations may choose to throw an exception, log a message, or perform some other side-effect based on the invalid value.
Used in conjunction with ValidationUtility.validate(Object, Predicate, Validator)
.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic String
formatMessage
(ErrorMessageKey errorMessageKey, Parameter parameter) Formats a localized error message using the specified key and parameter.handleInvalid
(T value) Handles an invalid value detected during validation.static <T> Validator
<T> logging
(LogLevel level, ErrorMessageKey messageKey, Parameter parameter) Returns a handler that logs a validation warning or error using the specified log level, message key, and parameter.static <T> Validator
<T> throwing
(ErrorMessageKey messageKey, Parameter parameter) Returns a handler that throws aValidationException
using the given error message key and parameter.static <T> Optional
<T> Validates a value using the given predicate and handler.
-
Field Details
-
BUNDLE_NAME
- See Also:
-
-
Method Details
-
validate
Validates a value using the given predicate and handler.If the predicate returns
true
, the handler is invoked to process the invalid value (e.g., throw or log). Otherwise, the valid value is returned as anOptional
.- Type Parameters:
T
- the type of the value- Parameters:
value
- the value to validatepredicate
- the condition indicating invalidityhandler
- the strategy for handling invalid values- Returns:
- an
Optional
containing the valid value, or empty if handled
-
formatMessage
Formats a localized error message using the specified key and parameter.Message templates are retrieved from the
exception.messages
resource bundle, and parameter placeholders are replaced usingMessageFormat
.- Parameters:
errorMessageKey
- the structured error keyparameter
- the parameter involved in the validation- Returns:
- the formatted, localized message
-
handleInvalid
Handles an invalid value detected during validation.- Parameters:
value
- the invalid value- Returns:
- an empty optional if handled, or a fallback value (if applicable)
-
throwing
Returns a handler that throws aValidationException
using the given error message key and parameter.- Type Parameters:
T
- the type of the value- Parameters:
messageKey
- the structured message keyparameter
- the parameter that failed validation- Returns:
- a throwing validation handler
-
logging
Returns a handler that logs a validation warning or error using the specified log level, message key, and parameter.- Type Parameters:
T
- the type of the value- Parameters:
level
- the severity level to logmessageKey
- the structured message keyparameter
- the parameter that failed validation- Returns:
- a logging validation handler
-