@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
90 lines • 3.93 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateValidator = void 0;
const Validator_1 = require("./Validator.cjs");
const constants_1 = require("./constants.cjs");
const decorators_1 = require("./decorators.cjs");
/**
* @description Validator for checking if a value is a valid date
* @summary 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".
* @param {string} [message] - Custom error message to display when validation fails, defaults to {@link DEFAULT_ERROR_MESSAGES#DATE}
* @class DateValidator
* @extends Validator
*
* @category Validators
* @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)
* ```
* @mermaid
* 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
*/
let DateValidator = class DateValidator extends Validator_1.Validator {
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.DATE) {
super(message, Number.name, Date.name, String.name);
}
/**
* @description Checks if the provided value is a valid date
* @summary 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.
*
* @param {Date | string} value - The value to validate, can be a Date object or a string
* @param {DateValidatorOptions} [options={}] - Optional configuration options for the validator
*
* @return {string | undefined} Error message if validation fails, undefined if validation passes
*
* @override
*
* @see Validator#hasErrors
*/
hasErrors(value, options = {}) {
if (value === undefined)
return;
if (typeof value === "string")
value = new Date(value);
if (Number.isNaN(value.getDate())) {
const { message = "" } = options;
return this.getMessage(message || this.message);
}
}
};
exports.DateValidator = DateValidator;
exports.DateValidator = DateValidator = __decorate([
(0, decorators_1.validator)(constants_1.ValidationKeys.DATE),
__metadata("design:paramtypes", [String])
], DateValidator);
//# sourceMappingURL=DateValidator.js.map