@@ -15,7 +15,7 @@ import { buildApi, formatDirectiveConfig } from '../directive';
1515const importZod = `import myzod from 'myzod'` ;
1616const anySchema = `definedNonNullAnySchema` ;
1717
18- export const ZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
18+ export const MyZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) => {
1919 const tsVisitor = new TsVisitor ( schema , config ) ;
2020
2121 const importTypes : string [ ] = [ ] ;
@@ -27,7 +27,6 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
2727 }
2828 return [ importZod ] ;
2929 } ,
30-
3130 initialEmit : ( ) : string =>
3231 '\n' +
3332 [
@@ -75,15 +74,16 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
7574 EnumTypeDefinition : ( node : EnumTypeDefinitionNode ) => {
7675 const enumname = tsVisitor . convertName ( node . name . value ) ;
7776 importTypes . push ( enumname ) ;
78- /*
77+ // z.enum are basically myzod.literals
7978 if ( config . enumsAsTypes ) {
8079 return new DeclarationBlock ( { } )
8180 . export ( )
82- .asKind('const ')
81+ . asKind ( 'type ' )
8382 . 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 ;
8585 }
86- */
86+
8787 return new DeclarationBlock ( { } )
8888 . export ( )
8989 . asKind ( 'const' )
@@ -117,7 +117,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
117117 if ( ! isNonNullType ( parentType ) ) {
118118 const arrayGen = `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
119119 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()` ;
121121 }
122122 return `myzod.array(${ maybeLazy ( type . type , gen ) } )` ;
123123 }
@@ -128,7 +128,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
128128 if ( isNamedType ( type ) ) {
129129 const gen = generateNameNodeMyZodSchema ( config , tsVisitor , schema , type . name ) ;
130130 if ( isListType ( parentType ) ) {
131- return `${ gen } .nullable()` ; //TODO: Test this later
131+ return `${ gen } .nullable()` ;
132132 }
133133 const appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
134134 if ( isNonNullType ( parentType ) ) {
@@ -139,15 +139,14 @@ const generateInputObjectFieldTypeMyZodSchema = (
139139 return appliedDirectivesGen ;
140140 }
141141 if ( isListType ( parentType ) ) {
142- return `${ appliedDirectivesGen } .nullable()` ; //TODO: Test this later
142+ return `${ appliedDirectivesGen } .nullable()` ;
143143 }
144- return `${ appliedDirectivesGen } .optional()` ; // to make it equivalent to nullish: `${appliedDirectivesGen}.optional().or( nullable() )`;
144+ return `${ appliedDirectivesGen } .optional(). nullable()` ;
145145 }
146146 console . warn ( 'unhandled type:' , type ) ;
147147 return '' ;
148148} ;
149149
150- // TODO: Find out how it works and implement it
151150const applyDirectives = (
152151 config : ValidationSchemaPluginConfig ,
153152 field : InputValueDefinitionNode ,
0 commit comments