Class UniqueValidator<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
AsyncValidator<T>
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 Summary
ConstructorsConstructorDescriptionUniqueValidator(Function<T, Boolean> existsChecker) Creates a UniqueValidator with the specified existence checker.UniqueValidator(Function<T, Boolean> existsChecker, String message) Creates a UniqueValidator with the specified existence checker and message. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the debounce delay in milliseconds.Returns the error message for this validator.setDebounce(int ms) Sets the debounce delay in milliseconds.validateAsync(T value, ValidationContext context) Performs asynchronous validation of the given value.withMessage(String message) Creates a copy with a custom message.
-
Constructor Details
-
UniqueValidator
Creates a UniqueValidator with the specified existence checker.- Parameters:
existsChecker- A function that returns true if the value already exists
-
UniqueValidator
Creates a UniqueValidator with the specified existence checker and message.- Parameters:
existsChecker- A function that returns true if the value already existsmessage- The error message when value is not unique
-
-
Method Details
-
validateAsync
Description copied from interface:AsyncValidatorPerforms 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:
validateAsyncin interfaceAsyncValidator<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:AsyncValidatorReturns 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:
getDebounceMsin interfaceAsyncValidator<T>- Returns:
- The debounce delay in milliseconds
-
setDebounce
Sets the debounce delay in milliseconds.- Parameters:
ms- The debounce delay- Returns:
- This validator for method chaining
-
getMessage
Description copied from interface:AsyncValidatorReturns the error message for this validator.- Specified by:
getMessagein interfaceAsyncValidator<T>- Returns:
- The error message
-
withMessage
Creates a copy with a custom message.- Parameters:
message- The error message- Returns:
- A new UniqueValidator with the custom message
-