@@ -7,7 +7,7 @@ import path from 'path';
77import  type  {  Project ,  SourceFile  }  from  'ts-morph' ; 
88import  {  upperCaseFirst  }  from  'upper-case-first' ; 
99import  {  computePrismaClientImport  }  from  './generator' ; 
10- import  {  AggregateOperationSupport ,  TransformerParams  }  from  './types' ; 
10+ import  {  AggregateOperationSupport ,  ObjectMode ,   TransformerParams  }  from  './types' ; 
1111
1212export  default  class  Transformer  { 
1313    name : string ; 
@@ -28,6 +28,7 @@ export default class Transformer {
2828    private  inputObjectTypes : PrismaDMMF . InputType [ ] ; 
2929    public  sourceFiles : SourceFile [ ]  =  [ ] ; 
3030    private  zmodel : Model ; 
31+     private  mode : ObjectMode ; 
3132
3233    constructor ( params : TransformerParams )  { 
3334        this . originalName  =  params . name  ??  '' ; 
@@ -40,6 +41,7 @@ export default class Transformer {
4041        this . project  =  params . project ; 
4142        this . inputObjectTypes  =  params . inputObjectTypes ; 
4243        this . zmodel  =  params . zmodel ; 
44+         this . mode  =  params . mode ; 
4345    } 
4446
4547    static  setOutputPath ( outPath : string )  { 
@@ -73,7 +75,12 @@ export default class Transformer {
7375    } 
7476
7577    generateImportZodStatement ( )  { 
76-         return  "import { z } from 'zod';\n" ; 
78+         let  r  =  "import { z } from 'zod';\n" ; 
79+         if  ( this . mode  ===  'strip' )  { 
80+             // import the additional `smartUnion` helper 
81+             r  +=  `import { smartUnion } from '@zenstackhq/runtime/zod-utils';\n` ; 
82+         } 
83+         return  r ; 
7784    } 
7885
7986    generateExportSchemaStatement ( name : string ,  schema : string )  { 
@@ -210,8 +217,19 @@ export default class Transformer {
210217
211218        const  opt  =  ! field . isRequired  ? '.optional()'  : '' ; 
212219
213-         let  resString  = 
214-             alternatives . length  ===  1  ? alternatives . join ( ',\r\n' )  : `z.union([${ alternatives . join ( ',\r\n' ) } ${ opt }  ; 
220+         let  resString : string ; 
221+ 
222+         if  ( alternatives . length  ===  1 )  { 
223+             resString  =  alternatives . join ( ',\r\n' ) ; 
224+         }  else  { 
225+             if  ( alternatives . some ( ( alt )  =>  alt . includes ( 'Unchecked' ) ) )  { 
226+                 // if the union is for combining checked and unchecked input types, use `smartUnion` 
227+                 // to parse with the best candidate at runtime 
228+                 resString  =  this . wrapWithSmartUnion ( ...alternatives )  +  `${ opt }  ; 
229+             }  else  { 
230+                 resString  =  `z.union([${ alternatives . join ( ',\r\n' ) } ${ opt }  ; 
231+             } 
232+         } 
215233
216234        if  ( field . isNullable )  { 
217235            resString  +=  '.nullable()' ; 
@@ -391,17 +409,6 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
391409        return  `${ modelName } ${ queryName }  ; 
392410    } 
393411
394-     wrapWithZodUnion ( zodStringFields : string [ ] )  { 
395-         let  wrapped  =  '' ; 
396- 
397-         wrapped  +=  'z.union([' ; 
398-         wrapped  +=  '\n' ; 
399-         wrapped  +=  '  '  +  zodStringFields . join ( ',' ) ; 
400-         wrapped  +=  '\n' ; 
401-         wrapped  +=  '])' ; 
402-         return  wrapped ; 
403-     } 
404- 
405412    wrapWithZodObject ( zodStringFields : string  |  string [ ] ,  mode  =  'strict' )  { 
406413        let  wrapped  =  '' ; 
407414
@@ -425,6 +432,14 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
425432        return  wrapped ; 
426433    } 
427434
435+     wrapWithSmartUnion ( ...schemas : string [ ] )  { 
436+         if  ( this . mode  ===  'strip' )  { 
437+             return  `smartUnion(z, [${ schemas . join ( ', ' ) }  ; 
438+         }  else  { 
439+             return  `z.union([${ schemas . join ( ', ' ) }  ; 
440+         } 
441+     } 
442+ 
428443    async  generateInputSchemas ( options : PluginOptions ,  zmodel : Model )  { 
429444        const  globalExports : string [ ]  =  [ ] ; 
430445
@@ -464,7 +479,7 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
464479                this . resolveSelectIncludeImportAndZodSchemaLine ( model ) ; 
465480
466481            let  imports  =  [ 
467-                 `import { z } from 'zod'` , 
482+                 this . generateImportZodStatement ( ) , 
468483                this . generateImportPrismaStatement ( options ) , 
469484                selectImport , 
470485                includeImport , 
@@ -523,7 +538,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
523538                    ) ; 
524539                } 
525540                const  dataSchema  =  generateUnchecked 
526-                     ? `z.union([${ modelName } ${ modelName }  
541+                     ? this . wrapWithSmartUnion ( 
542+                           `${ modelName }  , 
543+                           `${ modelName }  
544+                       ) 
527545                    : `${ modelName }  ; 
528546                const  fields  =  `${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } ${ dataSchema }  ; 
529547                codeBody  +=  `create: ${ this . wrapWithZodObject ( fields ,  mode ) }  ; 
@@ -568,7 +586,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
568586                    ) ; 
569587                } 
570588                const  dataSchema  =  generateUnchecked 
571-                     ? `z.union([${ modelName } ${ modelName }  
589+                     ? this . wrapWithSmartUnion ( 
590+                           `${ modelName }  , 
591+                           `${ modelName }  
592+                       ) 
572593                    : `${ modelName }  ; 
573594                const  fields  =  `${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } ${ dataSchema } ${ modelName }  ; 
574595                codeBody  +=  `update: ${ this . wrapWithZodObject ( fields ,  mode ) }  ; 
@@ -586,7 +607,10 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
586607                    ) ; 
587608                } 
588609                const  dataSchema  =  generateUnchecked 
589-                     ? `z.union([${ modelName } ${ modelName }  
610+                     ? this . wrapWithSmartUnion ( 
611+                           `${ modelName }  , 
612+                           `${ modelName }  
613+                       ) 
590614                    : `${ modelName }  ; 
591615                const  fields  =  `data: ${ dataSchema } ${ modelName }  ; 
592616                codeBody  +=  `updateMany: ${ this . wrapWithZodObject ( fields ,  mode ) }  ; 
@@ -606,10 +630,16 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
606630                    ) ; 
607631                } 
608632                const  createSchema  =  generateUnchecked 
609-                     ? `z.union([${ modelName } ${ modelName }  
633+                     ? this . wrapWithSmartUnion ( 
634+                           `${ modelName }  , 
635+                           `${ modelName }  
636+                       ) 
610637                    : `${ modelName }  ; 
611638                const  updateSchema  =  generateUnchecked 
612-                     ? `z.union([${ modelName } ${ modelName }  
639+                     ? this . wrapWithSmartUnion ( 
640+                           `${ modelName }  , 
641+                           `${ modelName }  
642+                       ) 
613643                    : `${ modelName }  ; 
614644                const  fields  =  `${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } ${ modelName } ${ createSchema } ${ updateSchema }  ; 
615645                codeBody  +=  `upsert: ${ this . wrapWithZodObject ( fields ,  mode ) }  ; 
0 commit comments