Class DateValidator<T>

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

public class DateValidator<T> extends Object implements Validator<T>
Validates date format and optionally date range.

DateValidator checks that a value is a valid date in the specified format and optionally validates that it falls within a specified range.

Null and empty values pass validation - use RequiredValidator to enforce non-null.

Usage:


 // Basic date format validation
 DateValidator<String> validator = new DateValidator<>("yyyy-MM-dd");
 validator.validate("2025-01-15", context);   // Valid
 validator.validate("01/15/2025", context);   // Invalid (wrong format)
 validator.validate("2025-13-45", context);   // Invalid (invalid date)

 // Different format
 DateValidator<String> usFormat = new DateValidator<>("MM/dd/yyyy");
 usFormat.validate("01/15/2025", context);    // Valid

 // With range validation
 DateValidator<String> futureOnly = new DateValidator<String>("yyyy-MM-dd")
     .minDate(LocalDate.now())
     .withMessage("Date must be today or in the future");

 DateValidator<String> pastYear = new DateValidator<String>("yyyy-MM-dd")
     .minDate(LocalDate.now().minusYears(1))
     .maxDate(LocalDate.now())
     .withMessage("Date must be within the past year");

 // Fluent in ValidatedInput
 new ValidatedInput<>(input, errorSpan)
     .required()
     .date("yyyy-MM-dd");
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • DateValidator

      public DateValidator()
      Creates a DateValidator with the default format (yyyy-MM-dd).
    • DateValidator

      public DateValidator(String format)
      Creates a DateValidator with the specified format.
      Parameters:
      format - The date format pattern (e.g., "yyyy-MM-dd", "MM/dd/yyyy")
    • DateValidator

      public DateValidator(String format, String message)
      Creates a DateValidator with format and custom message.
      Parameters:
      format - The date format pattern
      message - The error message
  • Method Details

    • minDate

      public DateValidator<T> minDate(LocalDate minDate)
      Sets the minimum allowed date (inclusive).
      Parameters:
      minDate - The minimum date
      Returns:
      This validator for method chaining
    • maxDate

      public DateValidator<T> maxDate(LocalDate maxDate)
      Sets the maximum allowed date (inclusive).
      Parameters:
      maxDate - The maximum date
      Returns:
      This validator for method chaining
    • range

      public DateValidator<T> range(LocalDate minDate, LocalDate maxDate)
      Sets the date range (inclusive on both ends).
      Parameters:
      minDate - The minimum date
      maxDate - The maximum date
      Returns:
      This validator for method chaining
    • futureOrPresent

      public DateValidator<T> futureOrPresent()
      Restricts to dates in the future (including today).
      Returns:
      This validator for method chaining
    • future

      public DateValidator<T> future()
      Restricts to dates strictly in the future (excluding today).
      Returns:
      This validator for method chaining
    • pastOrPresent

      public DateValidator<T> pastOrPresent()
      Restricts to dates in the past (including today).
      Returns:
      This validator for method chaining
    • past

      public DateValidator<T> past()
      Restricts to dates strictly in the past (excluding today).
      Returns:
      This validator for method chaining
    • 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 DateValidator<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
    • getFormat

      public String getFormat()
      Returns the date format pattern.
      Returns:
      The format pattern
    • getMinDate

      public LocalDate getMinDate()
      Returns the minimum allowed date.
      Returns:
      The minimum date, or null if not set
    • getMaxDate

      public LocalDate getMaxDate()
      Returns the maximum allowed date.
      Returns:
      The maximum date, or null if not set