Skip to content

Commit e9efe6c

Browse files
committed
update
1 parent 9f22caf commit e9efe6c

File tree

3 files changed

+154
-45
lines changed

3 files changed

+154
-45
lines changed

packages/schema/src/plugins/zod/generator.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
PluginError,
23
PluginGlobalOptions,
34
PluginOptions,
45
RUNTIME_PACKAGE,
@@ -54,6 +55,17 @@ export class ZodSchemaGenerator {
5455
ensureEmptyDir(output);
5556
Transformer.setOutputPath(output);
5657

58+
// options validation
59+
if (
60+
this.options.mode &&
61+
(typeof this.options.mode !== 'string' || !['strip', 'strict', 'passthrough'].includes(this.options.mode))
62+
) {
63+
throw new PluginError(
64+
name,
65+
`Invalid mode option: "${this.options.mode}". Must be one of 'strip', 'strict', or 'passthrough'.`
66+
);
67+
}
68+
5769
// calculate the models to be excluded
5870
const excludeModels = this.getExcludedModels();
5971

@@ -323,10 +335,17 @@ export class ZodSchemaGenerator {
323335
});
324336
});
325337

326-
if (this.options.strict !== false) {
327-
writer.writeLine(').strict();');
328-
} else {
329-
writer.writeLine(');');
338+
switch (this.options.mode) {
339+
case 'strip':
340+
// zod strips by default
341+
writer.writeLine(')');
342+
break;
343+
case 'passthrough':
344+
writer.writeLine(').passthrough();');
345+
break;
346+
default:
347+
writer.writeLine(').strict();');
348+
break;
330349
}
331350

332351
// relation fields

packages/schema/src/plugins/zod/transformer.ts

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export default class Transformer {
291291

292292
prepareObjectSchema(zodObjectSchemaFields: string[], options: PluginOptions) {
293293
const objectSchema = `${this.generateExportObjectSchemaStatement(
294-
this.addFinalWrappers({ zodStringFields: zodObjectSchemaFields }, options.strict !== false)
294+
this.wrapWithZodObject(zodObjectSchemaFields, options.mode as string)
295295
)}\n`;
296296

297297
const prismaImportStatement = this.generateImportPrismaStatement(options);
@@ -314,15 +314,6 @@ export default class Transformer {
314314
export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
315315
}
316316

317-
addFinalWrappers({ zodStringFields }: { zodStringFields: string[] }, strict = true) {
318-
const fields = [...zodStringFields];
319-
let result = this.wrapWithZodObject(fields);
320-
if (strict) {
321-
result = `${result}.strict()`;
322-
}
323-
return result;
324-
}
325-
326317
generateImportPrismaStatement(options: PluginOptions) {
327318
const prismaClientImportPath = computePrismaClientImport(
328319
path.resolve(Transformer.outputPath, './objects'),
@@ -411,14 +402,26 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
411402
return wrapped;
412403
}
413404

414-
wrapWithZodObject(zodStringFields: string | string[]) {
405+
wrapWithZodObject(zodStringFields: string | string[], mode = 'strict') {
415406
let wrapped = '';
416407

417408
wrapped += 'z.object({';
418409
wrapped += '\n';
419410
wrapped += ' ' + zodStringFields;
420411
wrapped += '\n';
421412
wrapped += '})';
413+
414+
switch (mode) {
415+
case 'strip':
416+
// zod strips by default
417+
break;
418+
case 'passthrough':
419+
wrapped += '.passthrough()';
420+
break;
421+
default:
422+
wrapped += '.strict()';
423+
break;
424+
}
422425
return wrapped;
423426
}
424427

@@ -468,6 +471,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
468471
];
469472
let codeBody = '';
470473
const operations: [string, string][] = [];
474+
const mode = (options.mode as string) ?? 'strict';
471475

472476
// OrderByWithRelationInput's name is different when "fullTextSearch" is enabled
473477
const orderByWithRelationInput = this.inputObjectTypes
@@ -480,7 +484,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
480484
imports.push(
481485
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`
482486
);
483-
codeBody += `findUnique: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema }),`;
487+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema`;
488+
codeBody += `findUnique: ${this.wrapWithZodObject(fields, mode)},`;
484489
operations.push(['findUnique', origModelName]);
485490
}
486491

@@ -491,7 +496,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
491496
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`,
492497
`import { ${modelName}ScalarFieldEnumSchema } from '../enums/${modelName}ScalarFieldEnum.schema'`
493498
);
494-
codeBody += `findFirst: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${modelName}ScalarFieldEnumSchema).optional() }),`;
499+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${modelName}ScalarFieldEnumSchema).optional()`;
500+
codeBody += `findFirst: ${this.wrapWithZodObject(fields, mode)},`;
495501
operations.push(['findFirst', origModelName]);
496502
}
497503

@@ -502,7 +508,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
502508
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`,
503509
`import { ${modelName}ScalarFieldEnumSchema } from '../enums/${modelName}ScalarFieldEnum.schema'`
504510
);
505-
codeBody += `findMany: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${modelName}ScalarFieldEnumSchema).optional() }),`;
511+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${modelName}ScalarFieldEnumSchema).optional()`;
512+
codeBody += `findMany: ${this.wrapWithZodObject(fields, mode)},`;
506513
operations.push(['findMany', origModelName]);
507514
}
508515

@@ -518,31 +525,35 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
518525
const dataSchema = generateUnchecked
519526
? `z.union([${modelName}CreateInputObjectSchema, ${modelName}UncheckedCreateInputObjectSchema])`
520527
: `${modelName}CreateInputObjectSchema`;
521-
codeBody += `create: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} data: ${dataSchema} }),`;
528+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} data: ${dataSchema}`;
529+
codeBody += `create: ${this.wrapWithZodObject(fields, mode)},`;
522530
operations.push(['create', origModelName]);
523531
}
524532

525533
if (createMany && supportCreateMany(zmodel)) {
526534
imports.push(
527535
`import { ${modelName}CreateManyInputObjectSchema } from '../objects/${modelName}CreateManyInput.schema'`
528536
);
529-
codeBody += `createMany: z.object({ data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]), skipDuplicates: z.boolean().optional() }),`;
537+
const fields = `data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]), skipDuplicates: z.boolean().optional()`;
538+
codeBody += `createMany: ${this.wrapWithZodObject(fields, mode)},`;
530539
operations.push(['createMany', origModelName]);
531540
}
532541

533542
if (deleteOne) {
534543
imports.push(
535544
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`
536545
);
537-
codeBody += `'delete': z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema }),`;
546+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema`;
547+
codeBody += `'delete': ${this.wrapWithZodObject(fields, mode)},`;
538548
operations.push(['delete', origModelName]);
539549
}
540550

541551
if (deleteMany) {
542552
imports.push(
543553
`import { ${modelName}WhereInputObjectSchema } from '../objects/${modelName}WhereInput.schema'`
544554
);
545-
codeBody += `deleteMany: z.object({ where: ${modelName}WhereInputObjectSchema.optional() }),`;
555+
const fields = `where: ${modelName}WhereInputObjectSchema.optional()`;
556+
codeBody += `deleteMany: ${this.wrapWithZodObject(fields, mode)},`;
546557
operations.push(['deleteMany', origModelName]);
547558
}
548559

@@ -559,7 +570,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
559570
const dataSchema = generateUnchecked
560571
? `z.union([${modelName}UpdateInputObjectSchema, ${modelName}UncheckedUpdateInputObjectSchema])`
561572
: `${modelName}UpdateInputObjectSchema`;
562-
codeBody += `update: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} data: ${dataSchema}, where: ${modelName}WhereUniqueInputObjectSchema }),`;
573+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} data: ${dataSchema}, where: ${modelName}WhereUniqueInputObjectSchema`;
574+
codeBody += `update: ${this.wrapWithZodObject(fields, mode)},`;
563575
operations.push(['update', origModelName]);
564576
}
565577

@@ -576,7 +588,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
576588
const dataSchema = generateUnchecked
577589
? `z.union([${modelName}UpdateManyMutationInputObjectSchema, ${modelName}UncheckedUpdateManyInputObjectSchema])`
578590
: `${modelName}UpdateManyMutationInputObjectSchema`;
579-
codeBody += `updateMany: z.object({ data: ${dataSchema}, where: ${modelName}WhereInputObjectSchema.optional() }),`;
591+
const fields = `data: ${dataSchema}, where: ${modelName}WhereInputObjectSchema.optional()`;
592+
codeBody += `updateMany: ${this.wrapWithZodObject(fields, mode)},`;
580593
operations.push(['updateMany', origModelName]);
581594
}
582595

@@ -598,7 +611,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
598611
const updateSchema = generateUnchecked
599612
? `z.union([${modelName}UpdateInputObjectSchema, ${modelName}UncheckedUpdateInputObjectSchema])`
600613
: `${modelName}UpdateInputObjectSchema`;
601-
codeBody += `upsert: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema, create: ${createSchema}, update: ${updateSchema} }),`;
614+
const fields = `${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema, create: ${createSchema}, update: ${updateSchema}`;
615+
codeBody += `upsert: ${this.wrapWithZodObject(fields, mode)},`;
602616
operations.push(['upsert', origModelName]);
603617
}
604618

@@ -644,9 +658,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
644658
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`
645659
);
646660

647-
codeBody += `aggregate: z.object({ where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), ${aggregateOperations.join(
661+
const fields = `where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), ${aggregateOperations.join(
648662
', '
649-
)} }),`;
663+
)}`;
664+
codeBody += `aggregate: ${this.wrapWithZodObject(fields, mode)},`;
650665
operations.push(['aggregate', modelName]);
651666
}
652667

@@ -657,9 +672,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
657672
`import { ${modelName}ScalarWhereWithAggregatesInputObjectSchema } from '../objects/${modelName}ScalarWhereWithAggregatesInput.schema'`,
658673
`import { ${modelName}ScalarFieldEnumSchema } from '../enums/${modelName}ScalarFieldEnum.schema'`
659674
);
660-
codeBody += `groupBy: z.object({ where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${modelName}OrderByWithAggregationInputObjectSchema, ${modelName}OrderByWithAggregationInputObjectSchema.array()]).optional(), having: ${modelName}ScalarWhereWithAggregatesInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), by: z.array(${modelName}ScalarFieldEnumSchema), ${aggregateOperations.join(
675+
const fields = `where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${modelName}OrderByWithAggregationInputObjectSchema, ${modelName}OrderByWithAggregationInputObjectSchema.array()]).optional(), having: ${modelName}ScalarWhereWithAggregatesInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), by: z.array(${modelName}ScalarFieldEnumSchema), ${aggregateOperations.join(
661676
', '
662-
)} }),`;
677+
)}`;
678+
codeBody += `groupBy: ${this.wrapWithZodObject(fields, mode)},`;
663679

664680
operations.push(['groupBy', origModelName]);
665681
}
@@ -674,7 +690,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
674690
`import { ${modelName}CountAggregateInputObjectSchema } from '../objects/${modelName}CountAggregateInput.schema'`
675691
);
676692

677-
codeBody += `count: z.object({ where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${modelName}ScalarFieldEnumSchema).optional(), select: z.union([ z.literal(true), ${modelName}CountAggregateInputObjectSchema ]).optional() })`;
693+
const fields = `where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${orderByWithRelationInput}ObjectSchema, ${orderByWithRelationInput}ObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${modelName}ScalarFieldEnumSchema).optional(), select: z.union([ z.literal(true), ${modelName}CountAggregateInputObjectSchema ]).optional()`;
694+
codeBody += `count: ${this.wrapWithZodObject(fields, mode)},`;
678695
operations.push(['count', origModelName]);
679696
}
680697

0 commit comments

Comments
 (0)