-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprocess-schema.js
More file actions
68 lines (59 loc) · 2.27 KB
/
process-schema.js
File metadata and controls
68 lines (59 loc) · 2.27 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
/**
* Keys that contain nested objects with named children (e.g., definitions, properties).
* @type {string[]}
*/
const NESTED_WITH_NAME = ["definitions", "properties"];
/**
* Keys that contain directly nested schema objects (e.g., items, additionalProperties, not).
* @type {string[]}
*/
const NESTED_DIRECT = ["items", "additionalProperties", "not"];
/**
* Keys that contain arrays of schema objects (e.g., oneOf, anyOf, allOf).
* @type {string[]}
*/
const NESTED_ARRAY = ["oneOf", "anyOf", "allOf"];
/**
* Recursively processes a JSON Schema using the visitor pattern.
*
* @param {{
* schema?: (schema: Record<string, any>, context?: Record<string, any>) => Record<string, any> | void,
* object?: (obj: Record<string, any>, context?: Record<string, any>) => Record<string, any> | void,
* array?: (collection: any[], context?: Record<string, any>) => void
* }} visitor - Visitor object with optional handlers for schema, object, and array nodes.
* @param {Record<string, any>} json - The JSON Schema node to process.
* @param {Record<string, any>} [context] - Optional shared context passed through recursive calls.
* @returns {Record<string, any>} - The processed schema node.
*/
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;