@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
53 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
const constants_1 = require("./constants.cjs");
const BaseValidator_1 = require("./BaseValidator.cjs");
/**
* @description
* Abstract class for defining synchronous validators.
*
* This class extends the base {@link BaseValidator} and enforces that any implementation of `hasErrors` must be synchronous.
*
* Use this when the validation process is immediate and does not require asynchronous operations.
*
* @example
* ```typescript
* // Example of a synchronous validator that checks if a number is greater than
* class GreaterThanValidator extends Validator<{ gt?: number }> {
* constructor(message: string = "Value must be greater than {0}") {
* super(message);
* }
*
* hasErrors(value: number, options?: { gt?: number }) {
* const minValue = options?.gt ?? 0;
* if (value <= minValue) {
* return this.getMessage();
* }
* return undefined;
* }
* }
*
* // Example usage:
* const validator = new GreaterThanValidator();
* const error = validator.hasErrors(10, { gt: 15 });
* if (error) {
* console.log('Value must be greater than 15')
* } else {
* console.log('Value is valid');
* }
* ```
*
* - If `value` is less than or equal to `gt`, returns the error message.
* - Otherwise, returns `undefined` indicating validation success.
*
* @see {@link BaseValidator} For the base validator.
* @see {@link ValidatorOptions} For the base validator options.
*/
class Validator extends BaseValidator_1.BaseValidator {
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.DEFAULT, ...acceptedTypes) {
super(false, message, ...acceptedTypes);
}
}
exports.Validator = Validator;
//# sourceMappingURL=Validator.js.map