UNPKG

structured-elements

Version:

A TypeScript package for modelling and validating data

38 lines 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureReferencedValidator = void 0; const referenceValidators_1 = require("../build/referenceValidators"); const constants_1 = require("../constants"); const string_1 = require("../is/string"); const isModelReference = (subject, api) => { return (0, string_1.isString)(subject) && api.registeredModels().has(subject); }; const ensureReferencedValidator = ({ api, container, }) => { const { structure, target } = container[constants_1.referenceToken]; if (isModelReference(target, api)) { const model = api.registeredModels().get(target); if (!model) { api.privateFunctions.debug(`ensureReferencedValidator failed because the model ${target} was not found in the registry.`, { api, container }); throw new Error(`ensureReferencedValidator failed because the model ${target} was not found in the registry. See console for details.`); } const validators = model.validators(); return validators[structure]; } const expectation = target; const cache = api.internalCache.validators.get(expectation); const cachedValidator = cache && cache[structure]; if (cachedValidator) { return cachedValidator; } const validators = (0, referenceValidators_1.buildReferenceValidators)({ api, expectation, }); api.internalCache.validators.set(container, // The map we store the validators in doesn't let us specify a generic // type for the Subject, so we have to cast it here. validators); return validators[structure]; }; exports.ensureReferencedValidator = ensureReferencedValidator; //# sourceMappingURL=referencedValidator.js.map