-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-zod.ts
302 lines (275 loc) · 9.87 KB
/
generate-zod.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
import {
Project,
SyntaxKind,
InterfaceDeclaration,
TypeAliasDeclaration,
EnumDeclaration,
ModuleDeclaration,
Type,
} from "ts-morph";
import { writeFileSync } from "fs";
// A lookup for enum types by their name (as declared in the Prisma types)
const enumSchemas = new Map<string, string>();
// A set of property names we want to filter out (e.g. Array prototype methods)
const arrayMethodNames = new Set([
"pop",
"push",
"concat",
"join",
"reverse",
"shift",
"slice",
"sort",
"splice",
"unshift",
"indexOf",
"lastIndexOf",
"every",
"some",
"forEach",
"find",
"findIndex",
"filter",
"map",
"reduce",
"reduceRight",
"includes",
"flat",
"flatMap",
]);
/**
* Generates a Zod schema for an enum declaration.
*
* It produces code like:
* export const enum_MyEnumSchema = z.enum(["A", "B", "C"] as const);
*
* And stores the mapping from the enum's name (e.g. "MyEnum") to the variable name "enum_MyEnumSchema".
*/
function generateEnumSchema(enumDecl: EnumDeclaration): string {
const enumName = enumDecl.getName();
const schemaVar = `enum_${enumName}Schema`;
// Gather enum member values. This assumes string enum members.
const members = enumDecl.getMembers().map((m) => m.getName());
const schemaCode = `export const ${schemaVar} = z.enum([${members
.map((m) => `"${m}"`)
.join(", ")}] as const);\n\n`;
// Store the mapping.
enumSchemas.set(enumName, schemaVar);
return schemaCode;
}
/**
* Recursively maps a ts-morph Type to a Zod schema string.
* A WeakSet is used to detect and break recursive cycles.
*/
function mapTypeToZod(type: Type, visited = new WeakSet<Type>()): string {
// Break recursion if this type has already been seen.
if (visited.has(type)) {
console.warn("Recursive type detected for: " + type.getText());
return "z.any()";
}
visited.add(type);
// *** NEW JSON CHECK ***
// If the type text includes "Json" (e.g. "Json", "Prisma.JsonValue", etc.), simply return z.any()
if (type.getText().includes("Json")) {
return "z.any()";
}
// Check if the type text contains a reference to $Enums.
const typeText = type.getText();
const enumMatch = typeText.match(/\$Enums\.(\w+)/);
if (enumMatch) {
const enumName = enumMatch[1]; // e.g. "enum_lang"
if (enumSchemas.has(enumName)) {
return enumSchemas.get(enumName)!;
} else {
console.warn(`Enum ${enumName} referenced in $Enums not found in enumSchemas map.`);
}
}
// Also check the alias symbol.
const aliasSymbol = type.getAliasSymbol();
if (aliasSymbol) {
const aliasName = aliasSymbol.getName();
if (enumSchemas.has(aliasName)) {
return enumSchemas.get(aliasName)!;
}
}
// Check the direct symbol.
const symbol = type.getSymbol();
if (symbol) {
const symName = symbol.getName();
if (enumSchemas.has(symName)) {
return enumSchemas.get(symName)!;
}
}
// Primitive types
if (type.isString()) return "z.string()";
if (type.isNumber()) return "z.number()";
if (type.isBoolean()) return "z.boolean()";
if (type.getText() === "Date") return "z.date()";
if (type.getText() === "bigint") return "z.bigint()";
// Array types (e.g., string[])
const arrayElementType = type.getArrayElementType();
if (arrayElementType) {
return `z.array(${mapTypeToZod(arrayElementType, visited)})`;
}
// Union types (including unions with null)
if (type.isUnion()) {
const unionTypes = type.getUnionTypes();
// Check if this union is an inline enum (all members are string literals)
if (unionTypes.every((t) => t.isStringLiteral())) {
const aliasSym = type.getAliasSymbol();
if (aliasSym && enumSchemas.has(aliasSym.getName())) {
return enumSchemas.get(aliasSym.getName())!;
}
const values = unionTypes.map((t) => t.getLiteralValue() as string);
return `z.enum([${values.map((v) => `"${v}"`).join(", ")}] as const)`;
}
// Handle unions that include null.
const hasNull = unionTypes.some((t) => t.isNull());
if (hasNull) {
const nonNullTypes = unionTypes.filter((t) => !t.isNull());
if (nonNullTypes.length === 1) {
return `${mapTypeToZod(nonNullTypes[0], visited)}.nullable()`;
} else {
const unionStrs = nonNullTypes.map((t) => mapTypeToZod(t, visited));
return `z.union([${unionStrs.join(", ")}]).nullable()`;
}
}
// Otherwise, a union of different types.
const unionStrs = unionTypes.map((t) => mapTypeToZod(t, visited));
return `z.union([${unionStrs.join(", ")}])`;
}
// If the type is an object, try to generate an inline z.object schema if it has properties.
if (type.isObject()) {
const properties = type.getProperties();
if (properties.length > 0) {
let inner = "z.object({\n";
properties.forEach((prop) => {
const propName = prop.getName();
// Filter out "weird" internal properties and array prototype methods.
if (propName.startsWith("__@") || arrayMethodNames.has(propName)) return;
const decls = prop.getDeclarations();
if (decls.length > 0) {
const propType = decls[0].getType();
// Heuristic for optional properties.
const isOptional =
(decls[0].getKind() === SyntaxKind.PropertySignature &&
(decls[0] as any).hasQuestionToken?.()) ||
propType.getText().includes("undefined");
inner += ` ${propName}: ${mapTypeToZod(propType, visited)}${isOptional ? ".optional()" : ""},\n`;
}
});
inner += "})";
return inner;
}
}
// Fallback.
return "z.any()";
}
/**
* Generates a Zod schema from an interface declaration.
*/
function generateZodSchemaFromInterface(interfaceDecl: InterfaceDeclaration): string {
const interfaceName = interfaceDecl.getName();
let schema = `export const ${interfaceName}Schema = z.object({\n`;
interfaceDecl.getProperties().forEach((prop) => {
const propName = prop.getName();
// Skip internal/array methods.
if (propName.startsWith("__@") || arrayMethodNames.has(propName)) return;
const isOptional = prop.hasQuestionToken();
const propType = prop.getType();
schema += ` ${propName}: ${mapTypeToZod(propType)}${isOptional ? ".optional()" : ""},\n`;
});
schema += `});\n\n`;
return schema;
}
/**
* Generates a Zod schema from a type alias declaration.
*/
function generateZodSchemaFromTypeAlias(typeAliasDecl: TypeAliasDeclaration): string {
const typeName = typeAliasDecl.getName();
const type = typeAliasDecl.getType();
if (type.isObject()) {
const properties = type.getProperties();
if (properties.length > 0) {
let schema = `export const ${typeName}Schema = z.object({\n`;
properties.forEach((prop) => {
const propName = prop.getName();
if (propName.startsWith("__@") || arrayMethodNames.has(propName)) return;
const decls = prop.getDeclarations();
if (decls.length > 0) {
const propType = decls[0].getType();
const isOptional =
(decls[0].getKind() === SyntaxKind.PropertySignature &&
(decls[0] as any).hasQuestionToken?.()) ||
propType.getText().includes("undefined");
schema += ` ${propName}: ${mapTypeToZod(propType)}${isOptional ? ".optional()" : ""},\n`;
}
});
schema += `});\n\n`;
return schema;
}
}
const zodType = mapTypeToZod(type);
return `export const ${typeName}Schema = ${zodType};\n\n`;
}
// Initialize ts-morph with your tsconfig.
const project = new Project({
tsConfigFilePath: "./tsconfig.json",
});
// Load the Prisma client types file (adjust path as needed)
const prismaFile = project.getSourceFile("./client/index.d.ts");
if (!prismaFile) {
console.error("Prisma client types file not found!");
process.exit(1);
}
// Start building the output with the Zod import.
let output = `import { z } from "zod";\n\n`;
// Process namespace declarations (look for the $Enums namespace)
// Use getDescendantsOfKind to get ModuleDeclaration nodes and remove any surrounding quotes.
const namespaceDeclarations = prismaFile
.getDescendantsOfKind(SyntaxKind.ModuleDeclaration)
.filter((decl: ModuleDeclaration) =>
decl.getName().replace(/['"]/g, "") === "$Enums"
);
namespaceDeclarations.forEach((ns: ModuleDeclaration) => {
// Process classic enum declarations (if any)
ns.getDescendantsOfKind(SyntaxKind.EnumDeclaration).forEach((enumDecl: EnumDeclaration) => {
output += generateEnumSchema(enumDecl);
});
// Process type aliases that represent enums (unions of string literals)
ns.getDescendantsOfKind(SyntaxKind.TypeAliasDeclaration).forEach((ta) => {
const unionTypes = ta.getType().getUnionTypes();
if (unionTypes.length > 0 && unionTypes.every(t => t.isStringLiteral())) {
const enumName = ta.getName();
const schemaVar = `enum_${enumName}Schema`;
const values = unionTypes.map(t => t.getLiteralValue() as string);
const schemaCode = `export const ${schemaVar} = z.enum([${values.map(v => `"${v}"`).join(", ")}] as const);\n\n`;
enumSchemas.set(enumName, schemaVar);
output += schemaCode;
}
});
});
// Process top-level exported declarations.
const exportedDeclarations = prismaFile.getExportedDeclarations();
exportedDeclarations.forEach((decls, name) => {
// Filter out declarations unlikely to be models.
if (
name.startsWith("Prisma") ||
name.endsWith("Input") ||
name.endsWith("Args") ||
name.endsWith("Payload") ||
name.startsWith("_")
) {
return;
}
decls.forEach((decl) => {
if (decl.getKind() === SyntaxKind.InterfaceDeclaration) {
output += generateZodSchemaFromInterface(decl as InterfaceDeclaration);
} else if (decl.getKind() === SyntaxKind.TypeAliasDeclaration) {
output += generateZodSchemaFromTypeAlias(decl as TypeAliasDeclaration);
}
});
});
// Write the generated schemas to a file.
writeFileSync("prisma-zod-schemas.ts", output);
console.log("Generated prisma-zod-schemas.ts");