UNPKG

1.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.link = void 0;
4var JSONSchema_1 = require("./types/JSONSchema");
5var lodash_1 = require("lodash");
6/**
7 * Traverses over the schema, giving each node a reference to its
8 * parent node. We need this for downstream operations.
9 */
10function link(schema, parent) {
11 if (parent === void 0) { parent = null; }
12 if (!Array.isArray(schema) && !lodash_1.isPlainObject(schema)) {
13 return schema;
14 }
15 // Handle cycles
16 if (schema.hasOwnProperty(JSONSchema_1.Parent)) {
17 return schema;
18 }
19 // Add a reference to this schema's parent
20 Object.defineProperty(schema, JSONSchema_1.Parent, {
21 enumerable: false,
22 value: parent,
23 writable: false
24 });
25 // Arrays
26 if (Array.isArray(schema)) {
27 schema.forEach(function (child) { return link(child, schema); });
28 }
29 // Objects
30 for (var key in schema) {
31 link(schema[key], schema);
32 }
33 return schema;
34}
35exports.link = link;
36//# sourceMappingURL=linker.js.map
\No newline at end of file