Interface Validator<T>

Type Parameters:
T - The type of value being validated
All Known Implementing Classes:
AlphanumericValidator, CreditCardValidator, DateValidator, EmailValidator, GroupedValidator, LengthValidator, NumericValidator, PatternValidator, PhoneValidator, RangeValidator, RequiredValidator, UrlValidator

public interface Validator<T>
Core interface for all validators in the Oorian validation framework.

Validators are responsible for checking if a value meets certain criteria and returning a ValidationResult indicating success or failure with appropriate messages.

Implementing Custom Validators:


 public class ZipCodeValidator implements Validator<String> {
     private static final Pattern ZIP_PATTERN = Pattern.compile("^\\d{5}(-\\d{4})?$");
     private String message = "Please enter a valid ZIP code";

     @Override
     public ValidationResult validate(String value, ValidationContext context) {
         if (value == null || value.isEmpty()) {
             return ValidationResult.valid();  // Use RequiredValidator for null checks
         }
         if (!ZIP_PATTERN.matcher(value).matches()) {
             return ValidationResult.invalid(message);
         }
         return ValidationResult.valid();
     }

     @Override
     public String getMessage() {
         return message;
     }

     @Override
     public Validator<String> withMessage(String message) {
         ZipCodeValidator copy = new ZipCodeValidator();
         copy.message = message;
         return copy;
     }
 }
 

Using Lambda-Based Validators:


 Validator<String> noSpaces = Validator.of(
     s -> !s.contains(" "),
     "Value cannot contain spaces"
 );
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Details

    • validate

      ValidationResult validate(T value, ValidationContext context)
      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.

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

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

      Validator<T> withMessage(String message)
      Creates a new validator with a custom error message.

      This method returns a new instance with the custom message, leaving the original validator unchanged.

      Parameters:
      message - The custom error message
      Returns:
      A new validator instance with the custom message
    • of

      static <T> Validator<T> of(Predicate<T> test, String message)
      Creates a simple validator from a predicate and message.

      This factory method provides a convenient way to create validators inline without implementing the full interface.

      Usage:

      
       Validator<String> noSpaces = Validator.of(
           s -> s != null && !s.contains(" "),
           "Value cannot contain spaces"
       );
      
       Validator<Integer> positive = Validator.of(
           n -> n != null && n > 0,
           "Value must be positive"
       );
       
      Type Parameters:
      T - The type of value being validated
      Parameters:
      test - The predicate to test values against (returns true if valid)
      message - The error message when validation fails
      Returns:
      A new Validator instance
    • notNull

      static <T> Validator<T> notNull(String message)
      Creates a validator that fails on null values.

      Unlike most validators which pass on null, this creates a validator that explicitly requires non-null values. For a full-featured required validator, use RequiredValidator.

      Type Parameters:
      T - The type of value being validated
      Parameters:
      message - The error message when value is null
      Returns:
      A new Validator instance that fails on null
    • groups

      default Class<? extends ValidationGroup>[] groups()
      Returns the validation groups this validator belongs to.

      By default, validators belong to DefaultGroups.Default, meaning they run in all validation scenarios. Override this to restrict a validator to specific groups.

      Returns:
      Array of validation group classes this validator belongs to
      See Also:
    • forGroups

      default Validator<T> forGroups(Class<? extends ValidationGroup>... groups)
      Creates a new validator with the specified validation groups.

      This method returns a wrapped validator that belongs to the specified groups. The original validator is unchanged.

      Usage:

      
       // Only validate when creating
       Validator<String> required = new RequiredValidator<String>()
           .forGroups(DefaultGroups.Create.class);
      
       // Validate for both Create and Registration
       Validator<String> required = new RequiredValidator<String>()
           .forGroups(DefaultGroups.Create.class, UserGroups.Registration.class);
       
      Parameters:
      groups - The validation groups this validator should belong to
      Returns:
      A new validator instance with the specified groups
      See Also: