Class UniqueValidator<T>

java.lang.Object
com.oorian.validation.validators.UniqueValidator<T>
Type Parameters:
T - The type of value being validated (typically String)
All Implemented Interfaces:
AsyncValidator<T>

public class UniqueValidator<T> extends Object implements AsyncValidator<T>
Async validator that checks if a value already exists using a provided function.

UniqueValidator is commonly used to check if usernames, email addresses, or other unique identifiers are already in use. It takes a function that checks for existence and returns true if the value exists (i.e., is NOT unique).

Null and empty values are considered valid (unique) - use RequiredValidator to enforce non-null values.

Usage:


 // Check if username is taken
 AsyncValidator<String> usernameUnique = new UniqueValidator<>(
     username -> userRepository.usernameExists(username),
     "This username is already taken"
 );

 // Check if email is registered
 AsyncValidator<String> emailUnique = new UniqueValidator<>(
     email -> userRepository.emailExists(email),
     "This email is already registered"
 );

 // With custom debounce
 UniqueValidator<String> codeUnique = new UniqueValidator<>(
     code -> promoCodeService.codeExists(code),
     "This code is not valid"
 ).setDebounce(300);

 // Use with AsyncValidatedInput
 AsyncValidatedInput<String> username = new AsyncValidatedInput<>(usernameInput, usernameError)
     .required()
     .addAsyncValidator(new UniqueValidator<>(
         value -> userService.usernameExists(value),
         "Username is already taken"
     ));
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • UniqueValidator

      public UniqueValidator(Function<T,Boolean> existsChecker)
      Creates a UniqueValidator with the specified existence checker.
      Parameters:
      existsChecker - A function that returns true if the value already exists
    • UniqueValidator

      public UniqueValidator(Function<T,Boolean> existsChecker, String message)
      Creates a UniqueValidator with the specified existence checker and message.
      Parameters:
      existsChecker - A function that returns true if the value already exists
      message - The error message when value is not unique
  • Method Details

    • validateAsync

      public ValidationResult validateAsync(T value, ValidationContext context)
      Description copied from interface: AsyncValidator
      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.

      Specified by:
      validateAsync in interface AsyncValidator<T>
      Parameters:
      value - The value to validate (may be null)
      context - The validation context for accessing other field values
      Returns:
      The validation result
    • getDebounceMs

      public int getDebounceMs()
      Description copied from interface: AsyncValidator
      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.

      Specified by:
      getDebounceMs in interface AsyncValidator<T>
      Returns:
      The debounce delay in milliseconds
    • setDebounce

      public UniqueValidator<T> setDebounce(int ms)
      Sets the debounce delay in milliseconds.
      Parameters:
      ms - The debounce delay
      Returns:
      This validator for method chaining
    • getMessage

      public String getMessage()
      Description copied from interface: AsyncValidator
      Returns the error message for this validator.
      Specified by:
      getMessage in interface AsyncValidator<T>
      Returns:
      The error message
    • withMessage

      public UniqueValidator<T> withMessage(String message)
      Creates a copy with a custom message.
      Parameters:
      message - The error message
      Returns:
      A new UniqueValidator with the custom message