-
Notifications
You must be signed in to change notification settings - Fork 18
/
eachSchema.ts
79 lines (73 loc) · 2.46 KB
/
eachSchema.ts
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
69
70
71
72
73
74
75
76
77
78
79
import { JsonSchema, JsonPointer } from "./types";
import { isObject } from "./utils/isObject";
export type EachSchemaCallback = (schema: JsonSchema, pointer: JsonPointer) => void;
function eachProperty(
property: string,
schema: JsonSchema,
callback: EachSchemaCallback,
pointer: JsonPointer
) {
const target = schema[property];
if (!isObject(target)) {
return;
}
Object.keys(target).forEach(key => {
if (Array.isArray(target[key])) {
// ignore depndencies list (of properties)
return;
}
if (key === "$defs") {
eachProperty("$defs", target[key], callback, `${pointer}/${property}/$defs`);
} else {
eachSchema(target[key], callback, `${pointer}/${property}/${key}`);
}
});
}
function eachItem(
property: string,
schema: JsonSchema,
callback: EachSchemaCallback,
pointer: JsonPointer
) {
const target = schema[property];
if (!Array.isArray(target)) {
return;
}
target.forEach((s, key) => eachSchema(s, callback, `${pointer}/${property}/${key}`));
}
export function eachSchema(
schema: JsonSchema,
callback: EachSchemaCallback,
pointer: JsonPointer = ""
) {
if (schema === undefined) {
return;
}
// @ts-expect-error untyped
if (callback(schema, pointer) === true) {
return;
}
if (!isObject(schema)) {
return;
}
eachProperty("properties", schema, callback, pointer);
eachProperty("patternProperties", schema, callback, pointer);
eachSchema(schema.not, callback, `${pointer}/not`);
eachSchema(schema.additionalProperties, callback, `${pointer}/additionalProperties`);
eachProperty("dependencies", schema, callback, pointer);
// items
isObject(schema.items) && eachSchema(schema.items, callback, `${pointer}/items`);
eachItem("items", schema, callback, pointer);
// additional items
eachSchema(schema.additionalItems, callback, `${pointer}/additionalItems`);
// dynamic schemas
eachItem("allOf", schema, callback, pointer);
eachItem("anyOf", schema, callback, pointer);
eachItem("oneOf", schema, callback, pointer);
eachSchema(schema.if, callback, `${pointer}/if`);
eachSchema(schema.then, callback, `${pointer}/then`);
eachSchema(schema.else, callback, `${pointer}/else`);
// definitions
eachProperty("definitions", schema, callback, pointer);
eachProperty("$defs", schema, callback, pointer);
}