/
openApiToZod.ts
314 lines (255 loc) · 10.4 KB
/
openApiToZod.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import type { ReferenceObject, SchemaObject } from "openapi3-ts";
import { match } from "ts-pattern";
import type { CodeMetaData, ConversionTypeContext } from "./CodeMeta";
import { CodeMeta } from "./CodeMeta";
import { isReferenceObject } from "./isReferenceObject";
import type { TemplateContext } from "./template-context";
import { isPrimitiveType, wrapWithQuotesIfNeeded } from "./utils";
type ConversionArgs = {
schema: SchemaObject | ReferenceObject;
ctx?: ConversionTypeContext | undefined;
meta?: CodeMetaData | undefined;
options?: TemplateContext["options"] | undefined;
};
/**
* @see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schemaObject
* @see https://github.com/colinhacks/zod
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
export function getZodSchema({ schema, ctx, meta: inheritedMeta, options }: ConversionArgs): CodeMeta {
if (!schema) {
throw new Error("Schema is required");
}
const code = new CodeMeta(schema, ctx, inheritedMeta);
const meta = {
parent: code.inherit(inheritedMeta?.parent),
referencedBy: [...code.meta.referencedBy],
};
const refsPath = code.meta.referencedBy
.slice(0, -1)
.map((prev) => (ctx ? ctx.resolver.resolveRef(prev.ref!).normalized : prev.ref!));
if (isReferenceObject(schema)) {
if (!ctx) throw new Error("Context is required");
const schemaName = ctx.resolver.resolveRef(schema.$ref)?.normalized;
// circular(=recursive) reference
if (refsPath.length > 1 && refsPath.includes(schemaName)) {
return code.assign(ctx.zodSchemaByName[code.ref!]!);
}
let result = ctx.zodSchemaByName[schema.$ref];
if (!result) {
const actualSchema = ctx.resolver.getSchemaByRef(schema.$ref);
if (!actualSchema) {
throw new Error(`Schema ${schema.$ref} not found`);
}
result = getZodSchema({ schema: actualSchema, ctx, meta, options }).toString();
}
if (ctx.zodSchemaByName[schemaName]) {
return code;
}
ctx.zodSchemaByName[schemaName] = result;
return code;
}
if (schema.oneOf) {
if (schema.oneOf.length === 1) {
return getZodSchema({ schema: schema.oneOf[0]!, ctx, meta, options });
}
return code.assign(
`z.union([${schema.oneOf.map((prop) => getZodSchema({ schema: prop, ctx, meta, options })).join(", ")}])`
);
}
// anyOf = oneOf but with 1 or more = `T extends oneOf ? T | T[] : never`
if (schema.anyOf) {
if (schema.anyOf.length === 1) {
return getZodSchema({ schema: schema.anyOf[0]!, ctx, meta, options });
}
const types = schema.anyOf.map((prop) => getZodSchema({ schema: prop, ctx, meta, options })).join(", ");
const oneOf = `z.union([${types}])`;
return code.assign(`z.union([${oneOf}, z.array(${oneOf})])`);
}
if (schema.allOf) {
if (schema.allOf.length === 1) {
return getZodSchema({ schema: schema.allOf[0]!, ctx, meta, options });
}
const types = schema.allOf.map((prop) => getZodSchema({ schema: prop, ctx, meta, options }));
const first = types.at(0)!;
const rest = types
.slice(1)
.map((type) => `and(${type.toString()})`)
.join("");
return code.assign(`${first.toString()}.${rest}`);
}
if (schema.type && isPrimitiveType(schema.type)) {
if (schema.enum) {
if (schema.type === "string") {
// eslint-disable-next-line sonarjs/no-nested-template-literals
return code.assign(`z.enum([${schema.enum.map((value) => `"${value}"`).join(", ")}])`);
}
return code.assign(
`z.union([${schema.enum
// eslint-disable-next-line sonarjs/no-nested-template-literals
.map((value) => `z.literal(${value === null ? "null" : `"${value}"`})`)
.join(", ")}])`
);
}
return code.assign(
match(schema.type)
.with("integer", () => "z.number()")
.with("string", () =>
match(schema.format)
.with("binary", () => "z.instanceof(File)")
.otherwise(() => "z.string()")
)
.otherwise((type) => `z.${type}()`)
);
}
if (schema.type === "array") {
if (schema.items) {
return code.assign(`z.array(${getZodSchema({ schema: schema.items, ctx, meta, options }).toString()})`);
}
return code.assign("z.array(z.any())");
}
if (schema.type === "object" || schema.properties || schema.additionalProperties) {
let additionalProps = "";
if (
(typeof schema.additionalProperties === "boolean" && schema.additionalProperties) ||
(typeof schema.additionalProperties === "object" && Object.keys(schema.additionalProperties).length === 0)
) {
additionalProps = ".passthrough()";
} else if (typeof schema.additionalProperties === "object") {
return code.assign(
`z.record(${getZodSchema({ schema: schema.additionalProperties, ctx, meta, options }).toString()})`
);
}
const hasRequiredArray = schema.required && schema.required.length > 0;
const isPartial = options?.withImplicitRequiredProps ? false : !schema.required?.length;
let properties = "{}";
if (schema.properties) {
const propsMap = Object.entries(schema.properties).map(([prop, propSchema]) => {
const propMetadata = {
...meta,
isRequired: isPartial
? true
: hasRequiredArray
? schema.required?.includes(prop)
: options?.withImplicitRequiredProps,
name: prop,
} as CodeMetaData;
const propCode =
getZodSchema({ schema: propSchema, ctx, meta: propMetadata, options }) +
getZodChain(propSchema as SchemaObject, propMetadata);
return [prop, propCode.toString()];
});
properties =
"{ " +
propsMap.map(([prop, propSchema]) => `${wrapWithQuotesIfNeeded(prop!)}: ${propSchema}`).join(", ") +
" }";
}
return code.assign(`z.object(${properties})${isPartial ? ".partial()" : ""}${additionalProps}`);
}
if (!schema.type) return code.assign("z.unknown()");
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Unsupported schema type: ${schema.type}`);
}
export const getZodChain = (schema: SchemaObject, meta?: CodeMetaData) => {
const chains: string[] = [];
match(schema.type)
.with("string", () => chains.push(getZodChainableStringValidations(schema)))
.with("number", "integer", () => chains.push(getZodChainableNumberValidations(schema)))
.with("array", () => chains.push(getZodChainableArrayValidations(schema)))
.otherwise(() => void 0);
const output = chains
.concat(getZodChainablePresence(schema, meta), getZodChainableDefault(schema))
.filter(Boolean)
.join(".");
return output ? `.${output}` : "";
};
const getZodChainablePresence = (schema: SchemaObject, meta?: CodeMetaData) => {
if (schema.nullable && !meta?.isRequired) {
return "nullish()";
}
if (schema.nullable) {
return "nullable()";
}
if (!meta?.isRequired) {
return "optional()";
}
return "";
};
// TODO OA prefixItems -> z.tuple
const getZodChainableDefault = (schema: SchemaObject) => {
if (schema.default) {
const value = typeof schema.default === "string" ? `"${schema.default}"` : schema.default;
return `default(${value})`;
}
return "";
};
const wrapPatternIfNeeded = (pattern: string) => {
if (pattern.startsWith("/") && pattern.endsWith("/")) {
return pattern;
}
return `/${pattern}/`;
};
const getZodChainableStringValidations = (schema: SchemaObject) => {
const validations: string[] = [];
if (schema.minLength && schema.maxLength) {
validations.push(`length(${schema.minLength})`);
} else if (schema.minLength) {
validations.push(`min(${schema.minLength})`);
} else if (schema.maxLength) {
validations.push(`max(${schema.maxLength})`);
}
if (schema.pattern) {
validations.push(`regex(${wrapPatternIfNeeded(schema.pattern)})`);
}
if (schema.format) {
const chain = match(schema.format)
.with("email", () => "email()")
.with("hostname", () => "url()")
.with("uri", () => "url()")
.with("uuid", () => "uuid()")
.otherwise(() => "");
if (chain) {
validations.push(chain);
}
}
return validations.join(".");
};
const getZodChainableNumberValidations = (schema: SchemaObject) => {
const validations: string[] = [];
if (schema.type === "integer") {
validations.push("int()");
}
if (schema.minimum) {
if (schema.exclusiveMinimum) {
validations.push(`gt(${schema.minimum})`);
} else {
validations.push(`gte(${schema.minimum})`);
}
} else if (typeof schema.exclusiveMinimum === "number") {
validations.push(`gt(${schema.exclusiveMinimum})`);
}
if (schema.maximum) {
if (schema.exclusiveMaximum) {
validations.push(`lt(${schema.maximum})`);
} else {
validations.push(`lte(${schema.maximum})`);
}
} else if (typeof schema.exclusiveMaximum === "number") {
validations.push(`lt(${schema.exclusiveMaximum})`);
}
if (schema.multipleOf) {
validations.push(`multipleOf(${schema.multipleOf})`);
}
return validations.join(".");
};
const getZodChainableArrayValidations = (schema: SchemaObject) => {
const validations: string[] = [];
if (schema.minItems && schema.maxItems) {
validations.push(`length(${schema.minItems})`);
} else if (schema.minItems) {
validations.push(`min(${schema.minItems})`);
} else if (schema.maxItems) {
validations.push(`max(${schema.maxItems})`);
}
return validations.join(".");
};