@@ -21,6 +21,7 @@ const anySchema = `definedNonNullAnySchema`;
21
21
22
22
export const ZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) : SchemaVisitor => {
23
23
const importTypes : string [ ] = [ ] ;
24
+ const enumDeclarations : string [ ] = [ ] ;
24
25
25
26
return {
26
27
buildImports : ( ) : string [ ] => {
@@ -53,6 +54,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
53
54
. asKind ( 'const' )
54
55
. withName ( `${ anySchema } ` )
55
56
. withContent ( `z.any().refine((v) => isDefinedNonNullAny(v))` ) . string ,
57
+ ...enumDeclarations ,
56
58
] . join ( '\n' ) ,
57
59
InputObjectTypeDefinition : {
58
60
leave : ( node : InputObjectTypeDefinitionNode ) => {
@@ -62,11 +64,22 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
62
64
63
65
const shape = node . fields ?. map ( field => generateFieldZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
64
66
65
- return new DeclarationBlock ( { } )
66
- . export ( )
67
- . asKind ( 'function' )
68
- . withName ( `${ name } Schema(): z.ZodObject<Properties<${ name } >>` )
69
- . withBlock ( [ indent ( `return z.object<Properties<${ name } >>({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
67
+ switch ( config . validationSchemaExportType ) {
68
+ case 'const' :
69
+ return new DeclarationBlock ( { } )
70
+ . export ( )
71
+ . asKind ( 'const' )
72
+ . withName ( `${ name } Schema: z.ZodObject<Properties<${ name } >>` )
73
+ . withContent ( [ 'z.object({' , shape , '})' ] . join ( '\n' ) ) . string ;
74
+
75
+ case 'function' :
76
+ default :
77
+ return new DeclarationBlock ( { } )
78
+ . export ( )
79
+ . asKind ( 'function' )
80
+ . withName ( `${ name } Schema(): z.ZodObject<Properties<${ name } >>` )
81
+ . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
82
+ }
70
83
} ,
71
84
} ,
72
85
ObjectTypeDefinition : {
@@ -77,18 +90,33 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
77
90
78
91
const shape = node . fields ?. map ( field => generateFieldZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
79
92
80
- return new DeclarationBlock ( { } )
81
- . export ( )
82
- . asKind ( 'function' )
83
- . withName ( `${ name } Schema(): z.ZodObject<Properties<${ name } >>` )
84
- . withBlock (
85
- [
86
- indent ( `return z.object<Properties<${ name } >>({` ) ,
87
- indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) ,
88
- shape ,
89
- indent ( '})' ) ,
90
- ] . join ( '\n' )
91
- ) . string ;
93
+ switch ( config . validationSchemaExportType ) {
94
+ case 'const' :
95
+ return new DeclarationBlock ( { } )
96
+ . export ( )
97
+ . asKind ( 'const' )
98
+ . withName ( `${ name } Schema: z.ZodObject<Properties<${ name } >>` )
99
+ . withContent (
100
+ [ `z.object({` , indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) , shape , '})' ] . join (
101
+ '\n'
102
+ )
103
+ ) . string ;
104
+
105
+ case 'function' :
106
+ default :
107
+ return new DeclarationBlock ( { } )
108
+ . export ( )
109
+ . asKind ( 'function' )
110
+ . withName ( `${ name } Schema(): z.ZodObject<Properties<${ name } >>` )
111
+ . withBlock (
112
+ [
113
+ indent ( `return z.object({` ) ,
114
+ indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) ,
115
+ shape ,
116
+ indent ( '})' ) ,
117
+ ] . join ( '\n' )
118
+ ) . string ;
119
+ }
92
120
} ) ,
93
121
} ,
94
122
EnumTypeDefinition : {
@@ -97,19 +125,21 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
97
125
const enumname = visitor . convertName ( node . name . value ) ;
98
126
importTypes . push ( enumname ) ;
99
127
100
- if ( config . enumsAsTypes ) {
101
- return new DeclarationBlock ( { } )
102
- . export ( )
103
- . asKind ( 'const' )
104
- . withName ( `${ enumname } Schema` )
105
- . withContent ( `z.enum([${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } ])` ) . string ;
106
- }
107
-
108
- return new DeclarationBlock ( { } )
109
- . export ( )
110
- . asKind ( 'const' )
111
- . withName ( `${ enumname } Schema` )
112
- . withContent ( `z.nativeEnum(${ enumname } )` ) . string ;
128
+ // hoist enum declarations
129
+ enumDeclarations . push (
130
+ config . enumsAsTypes
131
+ ? new DeclarationBlock ( { } )
132
+ . export ( )
133
+ . asKind ( 'const' )
134
+ . withName ( `${ enumname } Schema` )
135
+ . withContent ( `z.enum([${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } ])` )
136
+ . string
137
+ : new DeclarationBlock ( { } )
138
+ . export ( )
139
+ . asKind ( 'const' )
140
+ . withName ( `${ enumname } Schema` )
141
+ . withContent ( `z.nativeEnum(${ enumname } )` ) . string
142
+ ) ;
113
143
} ,
114
144
} ,
115
145
UnionTypeDefinition : {
@@ -206,27 +236,23 @@ const applyDirectives = (
206
236
const generateNameNodeZodSchema = ( config : ValidationSchemaPluginConfig , visitor : Visitor , node : NameNode ) : string => {
207
237
const converter = visitor . getNameNodeConverter ( node ) ;
208
238
209
- if ( converter ?. targetKind === 'InputObjectTypeDefinition' ) {
210
- const name = converter . convertName ( ) ;
211
- return `${ name } Schema()` ;
212
- }
213
-
214
- if ( converter ?. targetKind === 'ObjectTypeDefinition' ) {
215
- const name = converter . convertName ( ) ;
216
- return `${ name } Schema()` ;
217
- }
218
-
219
- if ( converter ?. targetKind === 'EnumTypeDefinition' ) {
220
- const name = converter . convertName ( ) ;
221
- return `${ name } Schema` ;
222
- }
223
-
224
- if ( converter ?. targetKind === 'UnionTypeDefinition' ) {
225
- const name = converter . convertName ( ) ;
226
- return `${ name } Schema()` ;
239
+ switch ( converter ?. targetKind ) {
240
+ case 'InputObjectTypeDefinition' :
241
+ case 'ObjectTypeDefinition' :
242
+ case 'UnionTypeDefinition' :
243
+ // using switch-case rather than if-else to allow for future expansion
244
+ switch ( config . validationSchemaExportType ) {
245
+ case 'const' :
246
+ return `${ converter . convertName ( ) } Schema` ;
247
+ case 'function' :
248
+ default :
249
+ return `${ converter . convertName ( ) } Schema()` ;
250
+ }
251
+ case 'EnumTypeDefinition' :
252
+ return `${ converter . convertName ( ) } Schema` ;
253
+ default :
254
+ return zod4Scalar ( config , visitor , node . value ) ;
227
255
}
228
-
229
- return zod4Scalar ( config , visitor , node . value ) ;
230
256
} ;
231
257
232
258
const maybeLazy = ( type : TypeNode , schema : string ) : string => {
0 commit comments