Interface Validator
- All Known Implementing Classes:
DefaultJakartaValidator, DefaultValidator
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Implementations of this interface are typically invoked by the ValidatingInterceptor, which is automatically
registered by the Fluxzero client. Validation occurs before the message is passed to the handler method.
A Validator is responsible for detecting constraint violations and producing a ValidationException if
applicable. It supports validation groups to selectively apply rules.
The SDK's default validator also supports constrained payload methods with contextual parameters when validation runs
inside a Fluxzero message context. For example, a payload method annotated with @AssertTrue may declare a
User, Message, Metadata, or custom parameter resolved by the configured parameter resolvers.
If a constrained method declares parameters that cannot be resolved for the current validation run, that method is
skipped instead of failing validation.
Usage
The validator may be used programmatically:validator.assertValid(new CreateUserCommand(...));
But more commonly, it is used implicitly:
- When the
ValidatingInterceptoris registered with theHandlerFactory - Or when using dependency injection or framework-level validation support
Custom Implementations
Custom validators can be created to support use cases like:- Jakarta Validation annotations (e.g.,
@NotNull,@Size) - Domain-specific validation rules
- Structural validation on incoming
web requests
This interface is designed to be functional and composable, enabling fluent use within client applications.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault booleanareParametersValid(Object target, Executable executable, Object[] arguments) Returns whether invocation arguments for a method or constructor satisfy declared constraints.default <T> TassertValid(T object, Class<?>... groups) Validates the given object and throws aValidationExceptionif it is invalid.default voidassertValidParameters(Object target, Executable executable, Object[] arguments) Validates invocation arguments for a method or constructor and throws when constraints are violated.default voidassertValidReturnValue(Object target, Executable executable, Object returnValue) Validates a method or constructor return value and throws when constraints are violated.default Optional<ValidationException> checkParameterValidity(Object target, Executable executable, Object[] arguments) Validates invocation arguments for a method or constructor and returns an optionalValidationExceptionwhen constraints are violated.default Optional<ValidationException> checkReturnValueValidity(Object target, Executable executable, Object returnValue) Validates a method or constructor return value and returns an optionalValidationExceptionwhen constraints are violated.checkValidity(T object, Class<?>... groups) Validates the given object and returns an optionalValidationExceptionif the object is invalid.default <T> Set<ConstraintViolation<T>> getConstraintViolations(T object, Class<?>... groups) Validates the given object and returns structured constraint violations, including their property paths, invalid values, messages, and constraint metadata.default booleanhasReturnValueValidation(Executable executable) Returns whether the executable declares return value constraints that this validator can evaluate.default booleanChecks whether the given object is valid according to the defined validation rules.
-
Method Details
-
checkValidity
Validates the given object and returns an optionalValidationExceptionif the object is invalid.- Type Parameters:
T- the type of object being validated- Parameters:
object- the object to validategroups- optional validation groups to apply- Returns:
- an
Optionalcontaining the validation error if validation failed, or empty if valid
-
getConstraintViolations
Validates the given object and returns structured constraint violations, including their property paths, invalid values, messages, and constraint metadata.- Type Parameters:
T- the type of object being validated- Parameters:
object- the object to validategroups- optional validation groups to apply- Returns:
- constraint violations found while validating the object
- Throws:
UnsupportedOperationException- if this validator only exposes formattedValidationExceptions
-
assertValid
Validates the given object and throws aValidationExceptionif it is invalid.- Type Parameters:
T- the type of object being validated- Parameters:
object- the object to validategroups- optional validation groups to apply- Returns:
- the original object if valid
- Throws:
ValidationException- if the object is invalid
-
isValid
-
checkParameterValidity
default Optional<ValidationException> checkParameterValidity(@Nullable Object target, Executable executable, Object[] arguments) Validates invocation arguments for a method or constructor and returns an optionalValidationExceptionwhen constraints are violated.- Parameters:
target- the method target instance (ignored for constructors)executable- the executable being invokedarguments- invocation arguments- Returns:
- an optional validation exception
-
assertValidParameters
default void assertValidParameters(@Nullable Object target, Executable executable, Object[] arguments) throws ValidationException Validates invocation arguments for a method or constructor and throws when constraints are violated.- Parameters:
target- the method target instance (ignored for constructors)executable- the executable being invokedarguments- invocation arguments- Throws:
ValidationException- if arguments are invalid
-
areParametersValid
default boolean areParametersValid(@Nullable Object target, Executable executable, Object[] arguments) Returns whether invocation arguments for a method or constructor satisfy declared constraints.- Parameters:
target- the method target instance (ignored for constructors)executable- the executable being invokedarguments- invocation arguments- Returns:
trueif valid,falseotherwise
-
hasReturnValueValidation
Returns whether the executable declares return value constraints that this validator can evaluate.- Parameters:
executable- the executable whose return value may be validated- Returns:
trueif return value validation should be performed
-
checkReturnValueValidity
default Optional<ValidationException> checkReturnValueValidity(@Nullable Object target, Executable executable, @Nullable Object returnValue) Validates a method or constructor return value and returns an optionalValidationExceptionwhen constraints are violated.- Parameters:
target- the method target instance (ignored for constructors)executable- the executable that produced the valuereturnValue- the value returned by the executable- Returns:
- an optional validation exception
-
assertValidReturnValue
default void assertValidReturnValue(@Nullable Object target, Executable executable, @Nullable Object returnValue) throws ValidationException Validates a method or constructor return value and throws when constraints are violated.- Parameters:
target- the method target instance (ignored for constructors)executable- the executable that produced the valuereturnValue- the value returned by the executable- Throws:
ValidationException- if the return value is invalid
-