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>

public class PatternValidator<T> extends Object implements 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 Details

    • PatternValidator

      public PatternValidator(String regex)
      Creates a PatternValidator with the specified regex string.
      Parameters:
      regex - The regular expression pattern
    • PatternValidator

      public PatternValidator(Pattern pattern)
      Creates a PatternValidator with the specified compiled pattern.
      Parameters:
      pattern - The compiled regular expression pattern
    • PatternValidator

      public PatternValidator(String regex, String message)
      Creates a PatternValidator with the specified regex string and message.
      Parameters:
      regex - The regular expression pattern
      message - The error message
  • Method Details

    • validate

      public ValidationResult validate(T value, ValidationContext context)
      Description copied from interface: Validator
      Validates the given value.

      Implementations should return ValidationResult.valid() when the value passes validation, or ValidationResult.invalid(String) with an appropriate message when it fails.

      Validators typically should not fail on null values - use RequiredValidator to enforce non-null requirements. This allows validators to be composed without unexpected failures.

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

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

      public PatternValidator<T> withMessage(String message)
      Description copied from interface: Validator
      Creates 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:
      withMessage in interface Validator<T>
      Parameters:
      message - The custom error message
      Returns:
      A new validator instance with the custom message
    • getPattern

      public Pattern getPattern()
      Returns the pattern used by this validator.
      Returns:
      The compiled pattern