Interface AsyncValidator<T>

Type Parameters:
T - The type of value being validated
All Known Implementing Classes:
UniqueValidator

public interface AsyncValidator<T>
Interface for asynchronous validators that perform server-side validation.

AsyncValidator is used for validation that requires server-side operations such as database lookups, API calls, or other time-consuming checks. Examples include:

  • Checking if a username is already taken
  • Verifying an email is not already registered
  • Validating a promo code against a database
  • Checking inventory availability

Async validators are executed in a worker thread to prevent blocking the main request thread. They support debouncing on the client side to prevent excessive server calls during rapid typing.

Usage:


 // Implement custom async validator
 public class UsernameAvailableValidator implements AsyncValidator<String>
 {
     private final UserRepository userRepository;

     public UsernameAvailableValidator(UserRepository userRepository)
     {
         this.userRepository = userRepository;
     }

     @Override
     public ValidationResult validateAsync(String value, ValidationContext context)
     {
         if (userRepository.usernameExists(value))
         {
             return ValidationResult.invalid("Username is already taken");
         }
         return ValidationResult.valid();
     }

     @Override
     public String getMessage()
     {
         return "Username is already taken";
     }
 }

 // Use with AsyncValidatedInput
 AsyncValidatedInput<String> username = new AsyncValidatedInput<>(usernameInput, usernameError)
     .required()
     .minLength(3)
     .addAsyncValidator(new UsernameAvailableValidator(userRepository))
     .setDebounce(500);
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Details

    • validateAsync

      ValidationResult validateAsync(T value, ValidationContext context)
      Performs asynchronous validation of the given value.

      This method is called in a background thread (via OorianWorkerThread) and may perform blocking operations such as database queries or API calls.

      Unlike regular validators, async validators should typically fail on null/empty values that represent a real validation condition (e.g., checking if empty username exists). For standard null checks, combine with RequiredValidator.

      Parameters:
      value - The value to validate (may be null)
      context - The validation context for accessing other field values
      Returns:
      The validation result
    • getDebounceMs

      default int getDebounceMs()
      Returns the debounce delay in milliseconds.

      The debounce delay prevents excessive validation requests during rapid typing. The default is 400ms, which balances responsiveness with server load.

      Returns:
      The debounce delay in milliseconds
    • getMessage

      String getMessage()
      Returns the error message for this validator.
      Returns:
      The error message