MCPcopy Create free account
hub / github.com/json-editor/json-editor / compileSchema

Function compileSchema

docs/scripts/ajv-validator.js:1736–1828  ·  view source on GitHub ↗
(sch)

Source from the content-addressed store, hash-verified

1734// let nodeCount = 0
1735// Compiles schema in SchemaEnv
1736 function compileSchema(sch) {
1737 // TODO refactor - remove compilations
1738 const _sch = getCompilingSchema.call(this, sch);
1739 if (_sch)
1740 return _sch;
1741 const rootId = (0, resolve_1.getFullPath)(this.opts.uriResolver, sch.root.baseId); // TODO if getFullPath removed 1 tests fails
1742 const { es5, lines } = this.opts.code;
1743 const { ownProperties } = this.opts;
1744 const gen = new codegen_1.CodeGen(this.scope, { es5, lines, ownProperties });
1745 let _ValidationError;
1746 if (sch.$async) {
1747 _ValidationError = gen.scopeValue("Error", {
1748 ref: validation_error_1.default,
1749 code: (0, codegen_1._) `require("ajv/dist/runtime/validation_error").default`,
1750 });
1751 }
1752 const validateName = gen.scopeName("validate");
1753 sch.validateName = validateName;
1754 const schemaCxt = {
1755 gen,
1756 allErrors: this.opts.allErrors,
1757 data: names_1.default.data,
1758 parentData: names_1.default.parentData,
1759 parentDataProperty: names_1.default.parentDataProperty,
1760 dataNames: [names_1.default.data],
1761 dataPathArr: [codegen_1.nil],
1762 dataLevel: 0,
1763 dataTypes: [],
1764 definedProperties: new Set(),
1765 topSchemaRef: gen.scopeValue("schema", this.opts.code.source === true
1766 ? { ref: sch.schema, code: (0, codegen_1.stringify)(sch.schema) }
1767 : { ref: sch.schema }),
1768 validateName,
1769 ValidationError: _ValidationError,
1770 schema: sch.schema,
1771 schemaEnv: sch,
1772 rootId,
1773 baseId: sch.baseId || rootId,
1774 schemaPath: codegen_1.nil,
1775 errSchemaPath: sch.schemaPath || (this.opts.jtd ? "" : "#"),
1776 errorPath: (0, codegen_1._) `""`,
1777 opts: this.opts,
1778 self: this,
1779 };
1780 let sourceCode;
1781 try {
1782 this._compilations.add(sch);
1783 (0, validate_1.validateFunctionCode)(schemaCxt);
1784 gen.optimize(this.opts.code.optimize);
1785 // gen.optimize(1)
1786 const validateCode = gen.toString();
1787 sourceCode = `${gen.scopeRefs(names_1.default.scope)}return ${validateCode}`;
1788 // console.log((codeSize += sourceCode.length), (nodeCount += gen.nodeCount))
1789 if (this.opts.code.process)
1790 sourceCode = this.opts.code.process(sourceCode, sch);
1791 // console.log("\n\n\n *** \n", sourceCode)
1792 const makeValidate = new Function(`${names_1.default.self}`, `${names_1.default.scope}`, sourceCode);
1793 const validate = makeValidate(this, this.scope.get());

Callers

nothing calls this directly

Calls 9

scopeValueMethod · 0.95
scopeNameMethod · 0.95
optimizeMethod · 0.95
toStringMethod · 0.95
scopeRefsMethod · 0.95
addMethod · 0.80
getMethod · 0.80
valueMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected