Class

MaxValidator

MaxValidator(messageopt)

Constructor

# new MaxValidator(messageopt)

The MaxValidator checks if a numeric value, date, or string is less than or equal to a specified maximum value. It supports comparing numbers directly, dates chronologically, and strings lexicographically. This validator is typically used with the @max decorator.

Validator for checking if a value is less than or equal to a maximum

sequenceDiagram participant C as Client participant V as MaxValidator C->>V: new MaxValidator(message) C->>V: hasErrors(value, options) alt value is undefined V-->>C: undefined (valid) else value is Date and max is not Date V->>V: Convert max to Date alt conversion fails V-->>C: Error: Invalid Max param end end alt value > max V-->>C: Error message else value <= max 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#MAX

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

Example
```typescript
// Create a max validator with default error message
const maxValidator = new MaxValidator();

// Create a max validator with custom error message
const customMaxValidator = new MaxValidator("Value must not exceed {0}");

// Validate a number
const numOptions = { max: 100, message: "Number too large" };
const numResult = maxValidator.hasErrors(50, numOptions); // undefined (valid)
const invalidNumResult = maxValidator.hasErrors(150, numOptions); // Returns error message (invalid)

// Validate a date
const dateOptions = { max: new Date(2023, 11, 31) };
const dateResult = maxValidator.hasErrors(new Date(2023, 5, 15), dateOptions); // undefined (valid)
```

Extends

Methods

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

Validates that the provided value does not exceed the maximum value specified in the options. For dates, it performs chronological comparison, converting string representations to Date objects if necessary. For numbers and strings, it performs direct comparison.

Checks if a value is less than or equal to a maximum

Parameters:
Name Type Description
value number | Date | string

The value to validate

options MaxValidatorOptions

Configuration options containing the maximum value

Overrides:
See:

View Source validation/Validators/MaxValidator.ts, line 125

Error message if validation fails, undefined if validation passes

string | undefined