UNPKG

4.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const scope_1 = require("../compile/codegen/scope");
4const code_1 = require("../compile/codegen/code");
5function standaloneCode(ajv, refsOrFunc) {
6 if (!ajv.opts.code.source) {
7 throw new Error("moduleCode: ajv instance must have code.source option");
8 }
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);
17 if (!v)
18 throw new Error(`moduleCode: no schema with id ${id}`);
19 return v;
20 }
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 if (ajv.opts.code.esm) {
26 // Always do named export as `validate` rather than the variable `n` which is `validateXX` for known export value
27 return `"use strict";${_n}export const validate = ${n};${_n}export default ${n};${_n}${vCode}`;
28 }
29 return `"use strict";${_n}module.exports = ${n};${_n}module.exports.default = ${n};${_n}${vCode}`;
30 }
31 function multiExportsCode(schemas, getValidateFunc) {
32 var _a;
33 const usedValues = {};
34 let code = (0, code_1._) `"use strict";`;
35 for (const name in schemas) {
36 const v = getValidateFunc(schemas[name]);
37 if (v) {
38 const vCode = validateCode(usedValues, v.source);
39 const exportSyntax = ajv.opts.code.esm
40 ? (0, code_1._) `export const ${(0, code_1.getEsmExportName)(name)}`
41 : (0, code_1._) `exports${(0, code_1.getProperty)(name)}`;
42 code = (0, code_1._) `${code}${_n}${exportSyntax} = ${(_a = v.source) === null || _a === void 0 ? void 0 : _a.validateName};${_n}${vCode}`;
43 }
44 }
45 return `${code}`;
46 }
47 function validateCode(usedValues, s) {
48 if (!s)
49 throw new Error('moduleCode: function does not have "source" property');
50 if (usedState(s.validateName) === scope_1.UsedValueState.Completed)
51 return code_1.nil;
52 setUsedState(s.validateName, scope_1.UsedValueState.Started);
53 const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode);
54 const code = new code_1._Code(`${scopeCode}${_n}${s.validateCode}`);
55 return s.evaluated ? (0, code_1._) `${code}${s.validateName}.evaluated = ${s.evaluated};${_n}` : code;
56 function refValidateCode(n) {
57 var _a;
58 const vRef = (_a = n.value) === null || _a === void 0 ? void 0 : _a.ref;
59 if (n.prefix === "validate" && typeof vRef == "function") {
60 const v = vRef;
61 return validateCode(usedValues, v.source);
62 }
63 else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") {
64 const { validate, validateName } = vRef;
65 if (!validateName)
66 throw new Error("ajv internal error");
67 const def = ajv.opts.code.es5 ? scope_1.varKinds.var : scope_1.varKinds.const;
68 const wrapper = (0, code_1._) `${def} ${n} = {validate: ${validateName}};`;
69 if (usedState(validateName) === scope_1.UsedValueState.Started)
70 return wrapper;
71 const vCode = validateCode(usedValues, validate === null || validate === void 0 ? void 0 : validate.source);
72 return (0, code_1._) `${wrapper}${_n}${vCode}`;
73 }
74 return undefined;
75 }
76 function usedState(name) {
77 var _a;
78 return (_a = usedValues[name.prefix]) === null || _a === void 0 ? void 0 : _a.get(name);
79 }
80 function setUsedState(name, state) {
81 const { prefix } = name;
82 const names = (usedValues[prefix] = usedValues[prefix] || new Map());
83 names.set(name, state);
84 }
85 }
86}
87module.exports = exports = standaloneCode;
88Object.defineProperty(exports, "__esModule", { value: true });
89exports.default = standaloneCode;
90//# sourceMappingURL=index.js.map
\No newline at end of file