UNPKG

@decaf-ts/decorator-validation

Version:
79 lines 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidatorRegistry = void 0; const Validator_js_1 = require("./Validator.cjs"); /** * @summary Base Implementation of a Validator Registry * * @prop {Validator[]} [validators] the initial validators to register * * @class ValidatorRegistry * @implements IValidatorRegistry<T> * * @category Validation */ class ValidatorRegistry { constructor(...validators) { this.cache = {}; this.customKeyCache = {}; this.register(...validators); } /** * @summary retrieves the custom keys */ getCustomKeys() { return Object.assign({}, this.customKeyCache); } /** * @summary retrieves the registered validators keys */ getKeys() { return Object.keys(this.cache); } /** * @summary Retrieves a validator * * @param {string} validatorKey one of the {@link ValidationKeys} * @return {Validator | undefined} the registered Validator or undefined if there is nono matching the provided key */ get(validatorKey) { if (!(validatorKey in this.cache)) return undefined; const classOrInstance = this.cache[validatorKey]; if (Validator_js_1.Validator.isValidator(classOrInstance)) return classOrInstance; const constructor = classOrInstance.default || classOrInstance; const instance = new constructor(); this.cache[validatorKey] = instance; return instance; } /** * @summary Registers the provided validators onto the registry * * @param {T[] | ValidatorDefinition[]} validator */ register(...validator) { validator.forEach((v) => { if (Validator_js_1.Validator.isValidator(v)) { // const k = if (v.validationKey in this.cache) return; this.cache[v.validationKey] = v; } else { const { validationKey, validator, save } = v; if (validationKey in this.cache) return; this.cache[validationKey] = validator; if (!save) return; const obj = {}; obj[validationKey.toUpperCase()] = validationKey; this.customKeyCache = Object.assign({}, this.customKeyCache, obj); } }); } } exports.ValidatorRegistry = ValidatorRegistry; //# sourceMappingURL=ValidatorRegistry.js.map //# sourceMappingURL=ValidatorRegistry.cjs.map