-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathoptimize-schemas.ts
executable file
·141 lines (115 loc) · 3.28 KB
/
optimize-schemas.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env ts-node-transpile-only
import { strict as assert } from "assert";
import fs from "fs";
import {
JSONSchema7,
JSONSchema7Definition,
JSONSchema7TypeName,
} from "json-schema";
import { format } from "prettier";
import {
pathToSchemas,
ensureArray,
isJsonSchemaObject,
forEachJsonFile,
parseArgv,
} from "./utils";
parseArgv(__filename, []);
const JSONSchema7TypeNameOrder = [
"string",
"number",
"integer",
"boolean",
"object",
"array",
"null",
] as const;
const standardizeTypeProperty = (
types: JSONSchema7TypeName[],
): JSONSchema7TypeName | JSONSchema7TypeName[] => {
if (types.length === 1) {
return types[0];
}
return JSONSchema7TypeNameOrder.filter((type) => types.includes(type));
};
const isNullType = (
object: JSONSchema7Definition,
): object is { type: "null" | ["null"] } & JSONSchema7 => {
if (typeof object === "boolean") {
return false;
}
if (Array.isArray(object.type)) {
return object.type.length === 1 && object.type[0] === "null";
}
return object.type === "null";
};
const addNullToObject = (object: JSONSchema7) => {
assert.ok(object.type, `object schema is missing type`);
object.type = ensureArray(object.type).concat("null");
if (object.const) {
object.enum = [object.const];
delete object.const;
}
if (object.enum) {
object.enum.push(null);
}
};
forEachJsonFile(pathToSchemas, async (pathToSchema) => {
const contents = JSON.parse(fs.readFileSync(pathToSchema, "utf-8"));
fs.writeFileSync(
pathToSchema,
await format(
JSON.stringify(contents, (key, value: unknown | JSONSchema7) => {
if (!isJsonSchemaObject(value)) {
return value;
}
if (value.type) {
if (Array.isArray(value.type)) {
value.type = standardizeTypeProperty(value.type);
if (
value.type.includes("null") &&
typeof value.enum !== "undefined" &&
!value.enum.includes(null)
) {
value.enum = [...value.enum, null];
}
}
if (
ensureArray(value.type).includes("object") &&
!("tsAdditionalProperties" in value)
) {
value.additionalProperties ||= false;
}
}
if (value.anyOf) {
assert.ok(!value.oneOf, "object has both oneOf & anyOf");
// we don't have any use for anyOf, while oneOf is stricter
value.oneOf = value.anyOf;
delete value.anyOf;
}
if (value.oneOf) {
// { "type": "null" } & { ... } can be combined as "type" supports an array
if (value.oneOf.some(isNullType)) {
const [notNullType] = value.oneOf.filter(
(object) => !isNullType(object),
);
assert.ok(
typeof notNullType !== "boolean",
"unexpected boolean in oneOf",
);
if (!notNullType.$ref && !notNullType.allOf) {
addNullToObject(notNullType);
return notNullType;
}
}
// "oneOf" is redundant if it's only got one schema
if (value.oneOf.length === 1) {
return value.oneOf[0];
}
}
return value;
}),
{ parser: "json" },
),
);
});