-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema.ts
More file actions
115 lines (101 loc) · 3.14 KB
/
json-schema.ts
File metadata and controls
115 lines (101 loc) · 3.14 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type {
JsonSchema7Type,
JsonSchema7ObjectType,
} from "zod-to-json-schema";
const capitalizeFromCamelCase = (camel: string): string => {
const words = camel
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ");
return words
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
};
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
export const flattenedProperties = (
schema: JsonSchema7Type,
seenSchemas = new WeakSet()
): JsonSchema7ObjectType["properties"] => {
if (seenSchemas.has(schema)) {
return {};
}
seenSchemas.add(schema);
if ("properties" in schema) {
return schema.properties as JsonSchema7ObjectType["properties"];
}
if ("allOf" in schema) {
return Object.fromEntries(
schema.allOf!.flatMap((subSchema) =>
Object.entries(
flattenedProperties(subSchema as JsonSchema7Type, seenSchemas)
)
)
);
}
if ("anyOf" in schema) {
const entries = schema.anyOf!.flatMap((subSchema) =>
Object.entries(
flattenedProperties(subSchema as JsonSchema7Type, seenSchemas)
)
);
return Object.fromEntries(entries);
}
return {};
};
/** For a union type, returns a list of pairs of properties which *shouldn't* be used together (because they don't appear in the same type variant) */
export const incompatiblePropertyPairs = (
schema: JsonSchema7Type
): Array<[string, string]> => {
if (!("anyOf" in schema)) return [];
const sets = schema.anyOf!.map((subSchema) => {
const keys = Object.keys(flattenedProperties(subSchema as JsonSchema7Type));
return { keys, set: new Set(keys) };
});
const compatibilityEntries = sets.flatMap(({ keys, set }) => {
return keys.map((key) => {
const compatibleKeys = sets
.filter((other) => other.set.has(key))
.flatMap((other) => other.keys);
return [key, new Set(compatibleKeys)] as const;
});
});
const allKeys = [...new Set(sets.flatMap(({ keys }) => keys))];
return compatibilityEntries.flatMap(([key, compatibleWith]) => {
return allKeys
.filter((other) => key < other && !compatibleWith.has(other))
.map((other): [string, string] => [key, other]);
});
};
/**
* Tries fairly hard to build a roughly human-readable description of a JSON Schema type.
*/
export const getDescription = (
schema: JsonSchema7Type,
seenSchemas = new WeakSet()
): string => {
if (seenSchemas.has(schema)) {
return "";
}
seenSchemas.add(schema);
if ("items" in schema) {
return [
getDescription(schema.items as JsonSchema7Type, seenSchemas),
"(array)",
]
.filter(Boolean)
.join(" ");
}
const entries = Object.entries(schema)
.filter(
([key]) => !["type", "default", "additionalProperties"].includes(key)
)
.sort(([a], [b]) =>
a === "description" ? -1 : b === "description" ? 1 : 0
)
.map(([key, value]) => {
if (key === "description") return String(value);
if (key === "properties") return `Object (JSON formatted)`;
return `${capitalizeFromCamelCase(key)}: ${String(value)}`;
});
return entries.join("; ") || "";
};