-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprocess-schema.js
More file actions
68 lines (55 loc) · 1.99 KB
/
process-schema.js
File metadata and controls
68 lines (55 loc) · 1.99 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @typedef {import("json-schema").JSONSchema} JSONSchema
*/
/**
* @typedef {Object.<string, JSONSchema | boolean>} ProcessContext
*/
/**
* @typedef {Object} SchemaVisitor
* @property {(schema: JSONSchema | boolean, context?: ProcessContext) => JSONSchema | boolean | void} [schema]
* @property {(obj: JSONSchema | boolean, context?: ProcessContext) => JSONSchema | boolean | void} [object]
* @property {(arr: (JSONSchema | boolean)[], context?: ProcessContext) => void} [array]
*/
const NESTED_WITH_NAME = ["definitions", "properties"];
const NESTED_DIRECT = ["items", "additionalProperties", "not"];
const NESTED_ARRAY = ["oneOf", "anyOf", "allOf"];
/**
* Recursively processes a JSON Schema using a visitor pattern.
*
* @param {SchemaVisitor} visitor
* @param {JSONSchema | boolean} json
* @param {ProcessContext} [context]
* @returns {JSONSchema | boolean}
*/
const processSchema = (visitor, json, context) => {
if (!json || typeof json !== "object") return json;
json = { ...json };
if (typeof visitor?.schema === "function") {
json = visitor.schema(json, context) || json;
}
for (const name of NESTED_WITH_NAME) {
if (name in json && 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 (name in json && json[name] && typeof json[name] === "object" && !Array.isArray(json[name])) {
json[name] = processSchema(visitor, json[name], context);
}
}
for (const name of NESTED_ARRAY) {
if (name in json && 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;