index.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const { prefix } = s.validateName;
  44. const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set());
  45. nameSet.add(s.validateName);
  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. const vCode = validateCode(usedValues, validate === null || validate === void 0 ? void 0 : validate.source);
  59. const def = ajv.opts.code.es5 ? scope_1.varKinds.var : scope_1.varKinds.const;
  60. return code_1._ `${def} ${n} = {validate: ${validateName}};${_n}${vCode}`;
  61. }
  62. return undefined;
  63. }
  64. }
  65. }
  66. exports.default = standaloneCode;
  67. //# sourceMappingURL=index.js.map