-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprocess-schema.js
More file actions
44 lines (35 loc) · 1.25 KB
/
process-schema.js
File metadata and controls
44 lines (35 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const NESTED_WITH_NAME = ["definitions", "properties"];
const NESTED_DIRECT = ["items", "additionalProperties", "not"];
const NESTED_ARRAY = ["oneOf", "anyOf", "allOf"];
const processSchema = (visitor, json, context) => {
if (!json || typeof json !== "object") return json; // safety check
json = { ...json };
if (typeof visitor?.schema === "function") {
json = visitor.schema(json, context) || json;
}
for (const name of NESTED_WITH_NAME) {
if (json[name] && typeof json[name] === "object" && !Array.isArray(json[name])) {
if (typeof visitor?.object === "function") {
json[name] = visitor.object(json[name], context) || json[name];
}
for (const key of Object.keys(json[name])) {
json[name][key] = processSchema(visitor, json[name][key], context);
}
}
}
for (const name of NESTED_DIRECT) {
if (json[name] && typeof json[name] === "object" && !Array.isArray(json[name])) {
json[name] = processSchema(visitor, json[name], context);
}
}
for (const name of NESTED_ARRAY) {
if (Array.isArray(json[name])) {
json[name] = json[name].map((item) => processSchema(visitor, item, context));
if (typeof visitor?.array === "function") {
visitor.array(json[name], context);
}
}
}
return json;
};
module.exports = processSchema;