@@ -15,7 +15,7 @@ import { buildApi, formatDirectiveConfig } from '../directive';
15
15
const importZod = `import myzod from 'myzod'` ;
16
16
const anySchema = `definedNonNullAnySchema` ;
17
17
18
- export const ZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
18
+ export const MyZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
19
19
const tsVisitor = new TsVisitor ( schema , config ) ;
20
20
21
21
const importTypes : string [ ] = [ ] ;
@@ -27,7 +27,6 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
27
27
}
28
28
return [ importZod ] ;
29
29
} ,
30
-
31
30
initialEmit : ( ) : string =>
32
31
'\n' +
33
32
[
@@ -75,15 +74,16 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
75
74
EnumTypeDefinition : ( node : EnumTypeDefinitionNode ) => {
76
75
const enumname = tsVisitor . convertName ( node . name . value ) ;
77
76
importTypes . push ( enumname ) ;
78
- /*
77
+ // z.enum are basically myzod.literals
79
78
if ( config . enumsAsTypes ) {
80
79
return new DeclarationBlock ( { } )
81
80
. export ( )
82
- .asKind('const ')
81
+ . asKind ( 'type ' )
83
82
. withName ( `${ enumname } Schema` )
84
- .withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`).string;
83
+ . withContent ( `myzod.literals(${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } )` )
84
+ . string ;
85
85
}
86
- */
86
+
87
87
return new DeclarationBlock ( { } )
88
88
. export ( )
89
89
. asKind ( 'const' )
@@ -117,7 +117,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
117
117
if ( ! isNonNullType ( parentType ) ) {
118
118
const arrayGen = `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
119
119
const maybeLazyGen = applyDirectives ( config , field , arrayGen ) ;
120
- return `${ maybeLazyGen } .optional()` ; // to make it equivalent to nullish: `${maybeLazyGen}.optional().optional().or( nullable() )`;
120
+ return `${ maybeLazyGen } .optional(). nullable()` ;
121
121
}
122
122
return `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
123
123
}
@@ -128,7 +128,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
128
128
if ( isNamedType ( type ) ) {
129
129
const gen = generateNameNodeMyZodSchema ( config , tsVisitor , schema , type . name ) ;
130
130
if ( isListType ( parentType ) ) {
131
- return `${ gen } .nullable()` ; //TODO: Test this later
131
+ return `${ gen } .nullable()` ;
132
132
}
133
133
const appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
134
134
if ( isNonNullType ( parentType ) ) {
@@ -139,15 +139,14 @@ const generateInputObjectFieldTypeMyZodSchema = (
139
139
return appliedDirectivesGen ;
140
140
}
141
141
if ( isListType ( parentType ) ) {
142
- return `${ appliedDirectivesGen } .nullable()` ; //TODO: Test this later
142
+ return `${ appliedDirectivesGen } .nullable()` ;
143
143
}
144
- return `${ appliedDirectivesGen } .optional()` ; // to make it equivalent to nullish: `${appliedDirectivesGen}.optional().or( nullable() )`;
144
+ return `${ appliedDirectivesGen } .optional(). nullable()` ;
145
145
}
146
146
console . warn ( 'unhandled type:' , type ) ;
147
147
return '' ;
148
148
} ;
149
149
150
- // TODO: Find out how it works and implement it
151
150
const applyDirectives = (
152
151
config : ValidationSchemaPluginConfig ,
153
152
field : InputValueDefinitionNode ,
0 commit comments