@@ -291,7 +291,7 @@ export default class Transformer {
291
291
292
292
prepareObjectSchema ( zodObjectSchemaFields : string [ ] , options : PluginOptions ) {
293
293
const objectSchema = `${ this . generateExportObjectSchemaStatement (
294
- this . addFinalWrappers ( { zodStringFields : zodObjectSchemaFields } )
294
+ this . wrapWithZodObject ( zodObjectSchemaFields , options . mode as string )
295
295
) } \n`;
296
296
297
297
const prismaImportStatement = this . generateImportPrismaStatement ( options ) ;
@@ -314,12 +314,6 @@ export default class Transformer {
314
314
export const ${ this . name } ObjectSchema: SchemaType = ${ schema } as SchemaType;` ;
315
315
}
316
316
317
- addFinalWrappers ( { zodStringFields } : { zodStringFields : string [ ] } ) {
318
- const fields = [ ...zodStringFields ] ;
319
-
320
- return this . wrapWithZodObject ( fields ) + '.strict()' ;
321
- }
322
-
323
317
generateImportPrismaStatement ( options : PluginOptions ) {
324
318
const prismaClientImportPath = computePrismaClientImport (
325
319
path . resolve ( Transformer . outputPath , './objects' ) ,
@@ -408,14 +402,26 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
408
402
return wrapped ;
409
403
}
410
404
411
- wrapWithZodObject ( zodStringFields : string | string [ ] ) {
405
+ wrapWithZodObject ( zodStringFields : string | string [ ] , mode = 'strict' ) {
412
406
let wrapped = '' ;
413
407
414
408
wrapped += 'z.object({' ;
415
409
wrapped += '\n' ;
416
410
wrapped += ' ' + zodStringFields ;
417
411
wrapped += '\n' ;
418
412
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
+ }
419
425
return wrapped ;
420
426
}
421
427
@@ -465,6 +471,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
465
471
] ;
466
472
let codeBody = '' ;
467
473
const operations : [ string , string ] [ ] = [ ] ;
474
+ const mode = ( options . mode as string ) ?? 'strict' ;
468
475
469
476
// OrderByWithRelationInput's name is different when "fullTextSearch" is enabled
470
477
const orderByWithRelationInput = this . inputObjectTypes
@@ -477,7 +484,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
477
484
imports . push (
478
485
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
479
486
) ;
480
- codeBody += `findUnique: z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereUniqueInputObjectSchema }),` ;
487
+ const fields = `${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereUniqueInputObjectSchema` ;
488
+ codeBody += `findUnique: ${ this . wrapWithZodObject ( fields , mode ) } ,` ;
481
489
operations . push ( [ 'findUnique' , origModelName ] ) ;
482
490
}
483
491
@@ -488,7 +496,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
488
496
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'` ,
489
497
`import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
490
498
) ;
491
- 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 ) } ,` ;
492
501
operations . push ( [ 'findFirst' , origModelName ] ) ;
493
502
}
494
503
@@ -499,7 +508,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
499
508
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'` ,
500
509
`import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
501
510
) ;
502
- 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 ) } ,` ;
503
513
operations . push ( [ 'findMany' , origModelName ] ) ;
504
514
}
505
515
@@ -515,31 +525,35 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
515
525
const dataSchema = generateUnchecked
516
526
? `z.union([${ modelName } CreateInputObjectSchema, ${ modelName } UncheckedCreateInputObjectSchema])`
517
527
: `${ modelName } CreateInputObjectSchema` ;
518
- codeBody += `create: z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } data: ${ dataSchema } }),` ;
528
+ const fields = `${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } data: ${ dataSchema } ` ;
529
+ codeBody += `create: ${ this . wrapWithZodObject ( fields , mode ) } ,` ;
519
530
operations . push ( [ 'create' , origModelName ] ) ;
520
531
}
521
532
522
533
if ( createMany && supportCreateMany ( zmodel ) ) {
523
534
imports . push (
524
535
`import { ${ modelName } CreateManyInputObjectSchema } from '../objects/${ modelName } CreateManyInput.schema'`
525
536
) ;
526
- 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 ) } ,` ;
527
539
operations . push ( [ 'createMany' , origModelName ] ) ;
528
540
}
529
541
530
542
if ( deleteOne ) {
531
543
imports . push (
532
544
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
533
545
) ;
534
- codeBody += `'delete': z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereUniqueInputObjectSchema }),` ;
546
+ const fields = `${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereUniqueInputObjectSchema` ;
547
+ codeBody += `'delete': ${ this . wrapWithZodObject ( fields , mode ) } ,` ;
535
548
operations . push ( [ 'delete' , origModelName ] ) ;
536
549
}
537
550
538
551
if ( deleteMany ) {
539
552
imports . push (
540
553
`import { ${ modelName } WhereInputObjectSchema } from '../objects/${ modelName } WhereInput.schema'`
541
554
) ;
542
- codeBody += `deleteMany: z.object({ where: ${ modelName } WhereInputObjectSchema.optional() }),` ;
555
+ const fields = `where: ${ modelName } WhereInputObjectSchema.optional()` ;
556
+ codeBody += `deleteMany: ${ this . wrapWithZodObject ( fields , mode ) } ,` ;
543
557
operations . push ( [ 'deleteMany' , origModelName ] ) ;
544
558
}
545
559
@@ -556,7 +570,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
556
570
const dataSchema = generateUnchecked
557
571
? `z.union([${ modelName } UpdateInputObjectSchema, ${ modelName } UncheckedUpdateInputObjectSchema])`
558
572
: `${ modelName } UpdateInputObjectSchema` ;
559
- 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 ) } ,` ;
560
575
operations . push ( [ 'update' , origModelName ] ) ;
561
576
}
562
577
@@ -573,7 +588,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
573
588
const dataSchema = generateUnchecked
574
589
? `z.union([${ modelName } UpdateManyMutationInputObjectSchema, ${ modelName } UncheckedUpdateManyInputObjectSchema])`
575
590
: `${ modelName } UpdateManyMutationInputObjectSchema` ;
576
- 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 ) } ,` ;
577
593
operations . push ( [ 'updateMany' , origModelName ] ) ;
578
594
}
579
595
@@ -595,7 +611,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
595
611
const updateSchema = generateUnchecked
596
612
? `z.union([${ modelName } UpdateInputObjectSchema, ${ modelName } UncheckedUpdateInputObjectSchema])`
597
613
: `${ modelName } UpdateInputObjectSchema` ;
598
- 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 ) } ,` ;
599
616
operations . push ( [ 'upsert' , origModelName ] ) ;
600
617
}
601
618
@@ -641,9 +658,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
641
658
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
642
659
) ;
643
660
644
- 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 (
645
662
', '
646
- ) } }),`;
663
+ ) } `;
664
+ codeBody += `aggregate: ${ this . wrapWithZodObject ( fields , mode ) } ,` ;
647
665
operations . push ( [ 'aggregate' , modelName ] ) ;
648
666
}
649
667
@@ -654,9 +672,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
654
672
`import { ${ modelName } ScalarWhereWithAggregatesInputObjectSchema } from '../objects/${ modelName } ScalarWhereWithAggregatesInput.schema'` ,
655
673
`import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
656
674
) ;
657
- 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 (
658
676
', '
659
- ) } }),`;
677
+ ) } `;
678
+ codeBody += `groupBy: ${ this . wrapWithZodObject ( fields , mode ) } ,` ;
660
679
661
680
operations . push ( [ 'groupBy' , origModelName ] ) ;
662
681
}
@@ -671,7 +690,8 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
671
690
`import { ${ modelName } CountAggregateInputObjectSchema } from '../objects/${ modelName } CountAggregateInput.schema'`
672
691
) ;
673
692
674
- 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 ) } ,` ;
675
695
operations . push ( [ 'count' , origModelName ] ) ;
676
696
}
677
697
0 commit comments