Package com.oorian.validation.validators
Class PatternValidator<T>
java.lang.Object
com.oorian.validation.validators.PatternValidator<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
Validator<T>
Validates that a value matches a regular expression pattern.
PatternValidator checks that a string value matches the specified regex pattern.
Null values pass validation - use RequiredValidator to enforce non-null.
Usage:
// Basic pattern
PatternValidator<String> alphanumeric = new PatternValidator<>("^[a-zA-Z0-9]+$");
// With custom message
PatternValidator<String> username = new PatternValidator<>("^[a-zA-Z0-9_]+$")
.withMessage("Username can only contain letters, numbers, and underscores");
// Using compiled pattern
Pattern phonePattern = Pattern.compile("^\\d{3}-\\d{3}-\\d{4}$");
PatternValidator<String> phone = new PatternValidator<>(phonePattern);
// Fluent in ValidatedInput
new ValidatedInput<>(input, errorSpan)
.required()
.pattern("^[a-zA-Z]+$", "Only letters are allowed");
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionPatternValidator(String regex) Creates a PatternValidator with the specified regex string.PatternValidator(String regex, String message) Creates a PatternValidator with the specified regex string and message.PatternValidator(Pattern pattern) Creates a PatternValidator with the specified compiled pattern. -
Method Summary
Modifier and TypeMethodDescriptionReturns the default error message for this validator.Returns the pattern used by this validator.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
PatternValidator
Creates a PatternValidator with the specified regex string.- Parameters:
regex- The regular expression pattern
-
PatternValidator
Creates a PatternValidator with the specified compiled pattern.- Parameters:
pattern- The compiled regular expression pattern
-
PatternValidator
Creates a PatternValidator with the specified regex string and message.- Parameters:
regex- The regular expression patternmessage- The error message
-
-
Method Details
-
validate
Description copied from interface:ValidatorValidates the given value.Implementations should return
ValidationResult.valid()when the value passes validation, orValidationResult.invalid(String)with an appropriate message when it fails.Validators typically should not fail on null values - use
RequiredValidatorto enforce non-null requirements. This allows validators to be composed without unexpected failures. -
getMessage
Description copied from interface:ValidatorReturns the default error message for this validator.- Specified by:
getMessagein interfaceValidator<T>- Returns:
- The error message
-
withMessage
Description copied from interface:ValidatorCreates a new validator with a custom error message.This method returns a new instance with the custom message, leaving the original validator unchanged.
- Specified by:
withMessagein interfaceValidator<T>- Parameters:
message- The custom error message- Returns:
- A new validator instance with the custom message
-
getPattern
Returns the pattern used by this validator.- Returns:
- The compiled pattern
-