UNPKG

@decaf-ts/decorator-validation

Version:
49 lines 1.66 kB
import { DEFAULT_ERROR_MESSAGES } from "./constants.js"; import { BaseValidator } from "./BaseValidator.js"; /** * @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. */ export class Validator extends BaseValidator { constructor(message = DEFAULT_ERROR_MESSAGES.DEFAULT, ...acceptedTypes) { super(false, message, ...acceptedTypes); } } //# sourceMappingURL=Validator.js.map