11/* eslint-disable @typescript-eslint/ban-ts-comment */
22import { DELEGATE_AUX_RELATION_PREFIX } from '@zenstackhq/runtime' ;
3+ import { upperCaseFirst } from '@zenstackhq/runtime/local-helpers' ;
34import {
45 getForeignKeyFields ,
56 getRelationBackLink ,
@@ -12,7 +13,6 @@ import {
1213import { DataModel , DataModelField , Enum , isDataModel , isEnum , isTypeDef , type Model } from '@zenstackhq/sdk/ast' ;
1314import { checkModelHasModelRelation , findModelByName , isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers' ;
1415import { supportCreateMany , type DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma' ;
15- import { upperCaseFirst } from '@zenstackhq/runtime/local-helpers' ;
1616import path from 'path' ;
1717import type { Project , SourceFile } from 'ts-morph' ;
1818import { computePrismaClientImport } from './generator' ;
@@ -38,6 +38,7 @@ export default class Transformer {
3838 public sourceFiles : SourceFile [ ] = [ ] ;
3939 private zmodel : Model ;
4040 private mode : ObjectMode ;
41+ private zodVersion : 'v3' | 'v4' ;
4142
4243 constructor ( params : TransformerParams ) {
4344 this . originalName = params . name ?? '' ;
@@ -51,6 +52,7 @@ export default class Transformer {
5152 this . inputObjectTypes = params . inputObjectTypes ;
5253 this . zmodel = params . zmodel ;
5354 this . mode = params . mode ;
55+ this . zodVersion = params . zodVersion ;
5456 }
5557
5658 static setOutputPath ( outPath : string ) {
@@ -103,7 +105,7 @@ export default class Transformer {
103105 }
104106
105107 generateImportZodStatement ( ) {
106- let r = " import { z } from 'zod';\n" ;
108+ let r = ` import { z } from 'zod/ ${ this . zodVersion } ';\n` ;
107109 if ( this . mode === 'strip' ) {
108110 // import the additional `smartUnion` helper
109111 r += `import { smartUnion } from '@zenstackhq/runtime/zod-utils';\n` ;
@@ -480,7 +482,7 @@ export default class Transformer {
480482 name = `${ name } Type` ;
481483 origName = `${ origName } Type` ;
482484 }
483- const outType = `z.ZodType< Prisma.${ origName } >` ;
485+ const outType = this . makeZodType ( ` Prisma.${ origName } ` ) ;
484486 return `type SchemaType = ${ outType } ;
485487export const ${ this . name } ObjectSchema: SchemaType = ${ schema } as SchemaType;` ;
486488 }
@@ -499,7 +501,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
499501 if ( this . hasJson ) {
500502 jsonSchemaImplementation += `\n` ;
501503 jsonSchemaImplementation += `const literalSchema = z.union([z.string(), z.number(), z.boolean()]);\n` ;
502- jsonSchemaImplementation += `const jsonSchema: z.ZodType< Prisma.InputJsonValue> = z.lazy(() =>\n` ;
504+ jsonSchemaImplementation += `const jsonSchema: ${ this . makeZodType ( ' Prisma.InputJsonValue' ) } = z.lazy(() =>\n` ;
503505 jsonSchemaImplementation += ` z.union([literalSchema, z.array(jsonSchema.nullable()), z.record(z.string(), jsonSchema.nullable())])\n` ;
504506 jsonSchemaImplementation += `);\n\n` ;
505507 }
@@ -886,9 +888,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
886888
887889 type ${ modelName } InputSchemaType = {
888890${ operations
889- . map ( ( [ operation , typeName ] ) =>
890- indentString ( `${ operation } : z.ZodType<Prisma.${ typeName } ${ upperCaseFirst ( operation ) } Args>` , 4 )
891- )
891+ . map ( ( [ operation , typeName ] ) => {
892+ const argType = `Prisma.${ typeName } ${ upperCaseFirst ( operation ) } Args` ;
893+ return indentString ( `${ operation } : ${ this . makeZodType ( argType ) } ` , 4 )
894+ } )
892895 . join ( ',\n' ) }
893896 }
894897
@@ -950,4 +953,8 @@ ${globalExports.join(';\n')}
950953 includeZodSchemaLineLazy,
951954 } ;
952955 }
956+
957+ private makeZodType ( typeArg : string ) {
958+ return this . zodVersion === 'v3' ? `z.ZodType<${ typeArg } >` : `z.ZodType<${ typeArg } , ${ typeArg } >` ;
959+ }
953960}
0 commit comments