1 | "use strict";
|
---|
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");
|
---|
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 | return `"use strict";${_n}module.exports = ${n};${_n}module.exports.default = ${n};${_n}${vCode}`;
|
---|
26 | }
|
---|
27 | function multiExportsCode(schemas, getValidateFunc) {
|
---|
28 | var _a;
|
---|
29 | const usedValues = {};
|
---|
30 | let code = code_1._ `"use strict";`;
|
---|
31 | for (const name in schemas) {
|
---|
32 | const v = getValidateFunc(schemas[name]);
|
---|
33 | if (v) {
|
---|
34 | const vCode = validateCode(usedValues, v.source);
|
---|
35 | code = code_1._ `${code}${_n}exports${code_1.getProperty(name)} = ${(_a = v.source) === null || _a === void 0 ? void 0 : _a.validateName};${_n}${vCode}`;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | return `${code}`;
|
---|
39 | }
|
---|
40 | function validateCode(usedValues, s) {
|
---|
41 | if (!s)
|
---|
42 | throw new Error('moduleCode: function does not have "source" property');
|
---|
43 | if (usedState(s.validateName) === scope_1.UsedValueState.Completed)
|
---|
44 | return code_1.nil;
|
---|
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 ? code_1._ `${code}${s.validateName}.evaluated = ${s.evaluated};${_n}` : code;
|
---|
49 | function refValidateCode(n) {
|
---|
50 | var _a;
|
---|
51 | const vRef = (_a = n.value) === null || _a === void 0 ? void 0 : _a.ref;
|
---|
52 | if (n.prefix === "validate" && typeof vRef == "function") {
|
---|
53 | const v = vRef;
|
---|
54 | return validateCode(usedValues, v.source);
|
---|
55 | }
|
---|
56 | else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") {
|
---|
57 | const { validate, validateName } = vRef;
|
---|
58 | if (!validateName)
|
---|
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 = code_1._ `${def} ${n} = {validate: ${validateName}};`;
|
---|
62 | if (usedState(validateName) === scope_1.UsedValueState.Started)
|
---|
63 | return wrapper;
|
---|
64 | const vCode = validateCode(usedValues, validate === null || validate === void 0 ? void 0 : validate.source);
|
---|
65 | return code_1._ `${wrapper}${_n}${vCode}`;
|
---|
66 | }
|
---|
67 | return undefined;
|
---|
68 | }
|
---|
69 | function usedState(name) {
|
---|
70 | var _a;
|
---|
71 | return (_a = usedValues[name.prefix]) === null || _a === void 0 ? void 0 : _a.get(name);
|
---|
72 | }
|
---|
73 | function setUsedState(name, state) {
|
---|
74 | const { prefix } = name;
|
---|
75 | const names = (usedValues[prefix] = usedValues[prefix] || new Map());
|
---|
76 | names.set(name, state);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | exports.default = standaloneCode;
|
---|
81 | //# sourceMappingURL=index.js.map |
---|