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.

@FunctionalInterface public interface Validator
Strategy interface for validating message payloads and other objects prior to handler invocation.

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 ValidatingInterceptor is registered with the HandlerFactory
  • 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 Details

    • checkValidity

      <T> Optional<ValidationException> checkValidity(T object, Class<?>... groups)
      Validates the given object and returns an optional ValidationException if the object is invalid.
      Type Parameters:
      T - the type of object being validated
      Parameters:
      object - the object to validate
      groups - optional validation groups to apply
      Returns:
      an Optional containing the validation error if validation failed, or empty if valid
    • getConstraintViolations

      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.
      Type Parameters:
      T - the type of object being validated
      Parameters:
      object - the object to validate
      groups - optional validation groups to apply
      Returns:
      constraint violations found while validating the object
      Throws:
      UnsupportedOperationException - if this validator only exposes formatted ValidationExceptions
    • assertValid

      default <T> T assertValid(T object, Class<?>... groups) throws ValidationException
      Validates the given object and throws a ValidationException if it is invalid.
      Type Parameters:
      T - the type of object being validated
      Parameters:
      object - the object to validate
      groups - optional validation groups to apply
      Returns:
      the original object if valid
      Throws:
      ValidationException - if the object is invalid
    • isValid

      default boolean isValid(Object object, Class<?>... groups)
      Checks whether the given object is valid according to the defined validation rules.
      Parameters:
      object - the object to validate
      groups - optional validation groups to apply
      Returns:
      true if the object is valid, false otherwise
    • checkParameterValidity

      default Optional<ValidationException> checkParameterValidity(@Nullable Object target, Executable executable, Object[] arguments)
      Validates invocation arguments for a method or constructor and returns an optional ValidationException when constraints are violated.
      Parameters:
      target - the method target instance (ignored for constructors)
      executable - the executable being invoked
      arguments - 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 invoked
      arguments - 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 invoked
      arguments - invocation arguments
      Returns:
      true if valid, false otherwise
    • hasReturnValueValidation

      default boolean hasReturnValueValidation(Executable executable)
      Returns whether the executable declares return value constraints that this validator can evaluate.
      Parameters:
      executable - the executable whose return value may be validated
      Returns:
      true if 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 optional ValidationException when constraints are violated.
      Parameters:
      target - the method target instance (ignored for constructors)
      executable - the executable that produced the value
      returnValue - 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 value
      returnValue - the value returned by the executable
      Throws:
      ValidationException - if the return value is invalid