2 Object.defineProperty(exports, "__esModule", { value: true });
3 const scope_1 = require("../compile/codegen/scope");
4 const code_1 = require("../compile/codegen/code");
5 function standaloneCode(ajv, refsOrFunc) {
6 if (!ajv.opts.code.source) {
7 throw new Error("moduleCode: ajv instance must have code.source option");
9 const { _n } = ajv.scope.opts;
10 return typeof refsOrFunc == "function"
11 ? funcExportCode(refsOrFunc.source)
12 : refsOrFunc !== undefined
13 ? multiExportsCode(refsOrFunc, getValidate)
14 : multiExportsCode(ajv.schemas, (sch) => sch.meta ? undefined : ajv.compile(sch.schema));
15 function getValidate(id) {
16 const v = ajv.getSchema(id);
18 throw new Error(`moduleCode: no schema with id ${id}`);
21 function funcExportCode(source) {
22 const usedValues = {};
23 const n = source === null || source === void 0 ? void 0 : source.validateName;
24 const vCode = validateCode(usedValues, source);
25 return `"use strict";${_n}module.exports = ${n};${_n}module.exports.default = ${n};${_n}${vCode}`;
27 function multiExportsCode(schemas, getValidateFunc) {
29 const usedValues = {};
30 let code = (0, code_1._) `"use strict";`;
31 for (const name in schemas) {
32 const v = getValidateFunc(schemas[name]);
34 const vCode = validateCode(usedValues, v.source);
35 code = (0, code_1._) `${code}${_n}exports${(0, code_1.getProperty)(name)} = ${(_a = v.source) === null || _a === void 0 ? void 0 : _a.validateName};${_n}${vCode}`;
40 function validateCode(usedValues, s) {
42 throw new Error('moduleCode: function does not have "source" property');
43 if (usedState(s.validateName) === scope_1.UsedValueState.Completed)
45 setUsedState(s.validateName, scope_1.UsedValueState.Started);
46 const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode);
47 const code = new code_1._Code(`${scopeCode}${_n}${s.validateCode}`);
48 return s.evaluated ? (0, code_1._) `${code}${s.validateName}.evaluated = ${s.evaluated};${_n}` : code;
49 function refValidateCode(n) {
51 const vRef = (_a = n.value) === null || _a === void 0 ? void 0 : _a.ref;
52 if (n.prefix === "validate" && typeof vRef == "function") {
54 return validateCode(usedValues, v.source);
56 else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") {
57 const { validate, validateName } = vRef;
59 throw new Error("ajv internal error");
60 const def = ajv.opts.code.es5 ? scope_1.varKinds.var : scope_1.varKinds.const;
61 const wrapper = (0, code_1._) `${def} ${n} = {validate: ${validateName}};`;
62 if (usedState(validateName) === scope_1.UsedValueState.Started)
64 const vCode = validateCode(usedValues, validate === null || validate === void 0 ? void 0 : validate.source);
65 return (0, code_1._) `${wrapper}${_n}${vCode}`;
69 function usedState(name) {
71 return (_a = usedValues[name.prefix]) === null || _a === void 0 ? void 0 : _a.get(name);
73 function setUsedState(name, state) {
74 const { prefix } = name;
75 const names = (usedValues[prefix] = usedValues[prefix] || new Map());
76 names.set(name, state);
80 module.exports = exports = standaloneCode;
81 Object.defineProperty(exports, "__esModule", { value: true });
82 exports.default = standaloneCode;
83 //# sourceMappingURL=index.js.map