Class

DateValidator

DateValidator(messageopt)

Constructor

# new DateValidator(messageopt)

The DateValidator checks if a value is a valid date object or a string that can be converted to a valid date. It validates that the value represents a real date and not an invalid date like "2023-02-31".

Validator for checking if a value is a valid date

sequenceDiagram participant C as Client participant V as DateValidator C->>V: new DateValidator(message) C->>V: hasErrors(value, options) alt value is undefined V-->>C: undefined (valid) else value is string V->>V: Convert to Date end alt Date is invalid (NaN) V-->>C: Error message else Date is valid V-->>C: undefined (valid) end
Parameters:
Name Type Attributes Description
message string <optional>

Custom error message to display when validation fails, defaults to DEFAULT_ERROR_MESSAGES#DATE

View Source validation/Validators/DateValidator.ts, line 11

Example
```typescript
// Create a date validator with default error message
const dateValidator = new DateValidator();

// Create a date validator with custom error message
const customDateValidator = new DateValidator("Please enter a valid date");

// Validate a date
const result = dateValidator.hasErrors(new Date()); // undefined (valid)
const invalidResult = dateValidator.hasErrors("not a date"); // Returns error message (invalid)
```

Extends

Methods

# hasErrors(value, optionsopt) → {string|undefined}

Validates that the given value is a valid date. If the value is a string, it attempts to convert it to a Date object. Returns an error message if the date is invalid, or undefined if the date is valid or if the value is undefined.

Checks if the provided value is a valid date

Parameters:
Name Type Attributes Default Description
value Date | string

The value to validate, can be a Date object or a string

options DateValidatorOptions <optional>
{}

Optional configuration options for the validator

Overrides:
See:

View Source validation/Validators/DateValidator.ts, line 107

Error message if validation fails, undefined if validation passes

string | undefined