UNPKG

@decaf-ts/decorator-validation

Version:
64 lines 2.64 kB
"use strict"; 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.OptionValidator = void 0; const Validator_1 = require("./Validator.cjs"); const constants_1 = require("./constants.cjs"); const decorators_1 = require("./decorators.cjs"); /** * @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_1.Validator { constructor(message = constants_1.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)); } } }; exports.OptionValidator = OptionValidator; exports.OptionValidator = OptionValidator = __decorate([ (0, decorators_1.validator)(constants_1.ValidationKeys.ENUM), __metadata("design:paramtypes", [String]) ], OptionValidator); //# sourceMappingURL=OptionValidator.js.map