Package com.oorian.validation.validators
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>
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 Summary
ConstructorsConstructorDescriptionCreates a DateValidator with the default format (yyyy-MM-dd).DateValidator(String format) Creates a DateValidator with the specified format.DateValidator(String format, String message) Creates a DateValidator with format and custom message. -
Method Summary
Modifier and TypeMethodDescriptionfuture()Restricts to dates strictly in the future (excluding today).Restricts to dates in the future (including today).Returns the date format pattern.Returns the maximum allowed date.Returns the default error message for this validator.Returns the minimum allowed date.Sets the maximum allowed date (inclusive).Sets the minimum allowed date (inclusive).past()Restricts to dates strictly in the past (excluding today).Restricts to dates in the past (including today).Sets the date range (inclusive on both ends).validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
DateValidator
public DateValidator()Creates a DateValidator with the default format (yyyy-MM-dd). -
DateValidator
Creates a DateValidator with the specified format.- Parameters:
format- The date format pattern (e.g., "yyyy-MM-dd", "MM/dd/yyyy")
-
DateValidator
Creates a DateValidator with format and custom message.- Parameters:
format- The date format patternmessage- The error message
-
-
Method Details
-
minDate
Sets the minimum allowed date (inclusive).- Parameters:
minDate- The minimum date- Returns:
- This validator for method chaining
-
maxDate
Sets the maximum allowed date (inclusive).- Parameters:
maxDate- The maximum date- Returns:
- This validator for method chaining
-
range
Sets the date range (inclusive on both ends).- Parameters:
minDate- The minimum datemaxDate- The maximum date- Returns:
- This validator for method chaining
-
futureOrPresent
Restricts to dates in the future (including today).- Returns:
- This validator for method chaining
-
future
Restricts to dates strictly in the future (excluding today).- Returns:
- This validator for method chaining
-
pastOrPresent
Restricts to dates in the past (including today).- Returns:
- This validator for method chaining
-
past
Restricts to dates strictly in the past (excluding today).- Returns:
- This validator for method chaining
-
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
-
getFormat
Returns the date format pattern.- Returns:
- The format pattern
-
getMinDate
Returns the minimum allowed date.- Returns:
- The minimum date, or null if not set
-
getMaxDate
Returns the maximum allowed date.- Returns:
- The maximum date, or null if not set
-