@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
104 lines • 4.32 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Validator } from "./Validator.js";
import { DEFAULT_ERROR_MESSAGES, ValidationKeys } from "./constants.js";
import { validator } from "./decorators.js";
/**
* @description Validator for checking if a value is less than or equal to a maximum
* @summary 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.
*
* @param {string} [message] - Custom error message to display when validation fails, defaults to {@link DEFAULT_ERROR_MESSAGES#MAX}
*
* @class MaxValidator
* @extends Validator
*
* @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)
* ```
*
* @mermaid
* 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
*
* @category Validators
*/
let MaxValidator = class MaxValidator extends Validator {
constructor(message = DEFAULT_ERROR_MESSAGES.MAX) {
super(message, "number", "Date", "string");
}
/**
* @description Checks if a value is less than or equal to a maximum
* @summary 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.
*
* @param {number | Date | string} value - The value to validate
* @param {MaxValidatorOptions} options - Configuration options containing the maximum value
*
* @return {string | undefined} Error message if validation fails, undefined if validation passes
*
* @override
*
* @see Validator#hasErrors
*/
hasErrors(value, options) {
if (typeof value === "undefined")
return;
let { max } = options;
if (value instanceof Date && !(max instanceof Date)) {
max = new Date(max);
if (Number.isNaN(max.getDate()))
throw new Error("Invalid Max param defined");
}
return value > max
? this.getMessage(options.message || this.message, max)
: undefined;
}
};
MaxValidator = __decorate([
validator(ValidationKeys.MAX),
__metadata("design:paramtypes", [String])
], MaxValidator);
export { MaxValidator };
//# sourceMappingURL=MaxValidator.js.map