UNPKG

@decaf-ts/decorator-validation

Version:
61 lines 2.45 kB
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"; /** * @summary Option Validator * @description Validates properties against an object or a list of accepted values * * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#ENUM} * * @class OptionValidator * @extends Validator * * @category Validators */ let OptionValidator = class OptionValidator extends Validator { constructor(message = DEFAULT_ERROR_MESSAGES.ENUM) { super(message); } /** * * @param {any[] | Record<any, any>} value * @param {EnumValidatorOptions} options * * @return {string | undefined} * * @memberOf module:decorator-validation * @override * * @see Validator#hasErrors */ hasErrors(value, options) { if (typeof value === "undefined") return; if (Array.isArray(options.enum)) return !options.enum.includes(value) ? this.getMessage(options.message || this.message, options.enum) : undefined; if (typeof options.enum === "object") { const keys = Object.keys(options.enum); for (const key of keys) if (options.enum[key] === value) return undefined; return this.getMessage(options.message || this.message, Object.values(options.enum)); } } }; OptionValidator = __decorate([ validator(ValidationKeys.ENUM), __metadata("design:paramtypes", [String]) ], OptionValidator); export { OptionValidator }; //# sourceMappingURL=OptionValidator.js.map