@@ -91,50 +91,65 @@ const generateInputObjectFieldYupSchema = (
91
91
field : InputValueDefinitionNode ,
92
92
indentCount : number
93
93
) : string => {
94
- let gen = generateInputObjectFieldTypeZodSchema ( config , tsVisitor , schema , field . type ) ;
95
- if ( config . directives && field . directives ) {
96
- const formatted = formatDirectiveConfig ( config . directives ) ;
97
- gen += buildApi ( formatted , field . directives ) ;
98
- }
94
+ const gen = generateInputObjectFieldTypeZodSchema ( config , tsVisitor , schema , field , field . type ) ;
99
95
return indent ( `${ field . name . value } : ${ maybeLazy ( field . type , gen ) } ` , indentCount ) ;
100
96
} ;
101
97
102
98
const generateInputObjectFieldTypeZodSchema = (
103
99
config : ValidationSchemaPluginConfig ,
104
100
tsVisitor : TsVisitor ,
105
101
schema : GraphQLSchema ,
102
+ field : InputValueDefinitionNode ,
106
103
type : TypeNode ,
107
104
parentType ?: TypeNode
108
105
) : string => {
109
106
if ( isListType ( type ) ) {
110
- const gen = generateInputObjectFieldTypeZodSchema ( config , tsVisitor , schema , type . type , type ) ;
107
+ const gen = generateInputObjectFieldTypeZodSchema ( config , tsVisitor , schema , field , type . type , type )
111
108
if ( ! isNonNullType ( parentType ) ) {
112
- return `z.array(${ maybeLazy ( type . type , gen ) } ).nullish()` ;
109
+ const arrayGen = `z.array(${ maybeLazy ( type . type , gen ) } )`
110
+ const maybeLazyGen = applyDirectives ( config , field , arrayGen )
111
+ return `${ maybeLazyGen } .nullish()` ;
113
112
}
114
113
return `z.array(${ maybeLazy ( type . type , gen ) } )` ;
115
114
}
116
115
if ( isNonNullType ( type ) ) {
117
- const gen = generateInputObjectFieldTypeZodSchema ( config , tsVisitor , schema , type . type , type ) ;
116
+ const gen = generateInputObjectFieldTypeZodSchema ( config , tsVisitor , schema , field , type . type , type ) ;
118
117
return maybeLazy ( type . type , gen ) ;
119
118
}
120
119
if ( isNamedType ( type ) ) {
121
- const gen = generateNameNodeZodSchema ( config , tsVisitor , schema , type . name ) ;
120
+ const gen = generateNameNodeZodSchema ( config , tsVisitor , schema , type . name )
121
+ if ( isListType ( parentType ) ) {
122
+ return `${ gen } .nullable()` ;
123
+ }
124
+ const appliedDirectivesGen = applyDirectives ( config , field , gen )
122
125
if ( isNonNullType ( parentType ) ) {
123
126
if ( config . notAllowEmptyString === true ) {
124
127
const tsType = tsVisitor . scalars [ type . name . value ] ;
125
128
if ( tsType === 'string' ) return `${ gen } .min(1)` ;
126
129
}
127
- return gen ;
130
+ return appliedDirectivesGen ;
128
131
}
129
132
if ( isListType ( parentType ) ) {
130
- return `${ gen } .nullable()` ;
133
+ return `${ appliedDirectivesGen } .nullable()` ;
131
134
}
132
- return `${ gen } .nullish()` ;
135
+ return `${ appliedDirectivesGen } .nullish()` ;
133
136
}
134
137
console . warn ( 'unhandled type:' , type ) ;
135
138
return '' ;
136
139
} ;
137
140
141
+ const applyDirectives = (
142
+ config : ValidationSchemaPluginConfig ,
143
+ field : InputValueDefinitionNode ,
144
+ gen : string ,
145
+ ) : string => {
146
+ if ( config . directives && field . directives ) {
147
+ const formatted = formatDirectiveConfig ( config . directives ) ;
148
+ return gen + buildApi ( formatted , field . directives ) ;
149
+ }
150
+ return gen
151
+ }
152
+
138
153
const generateNameNodeZodSchema = (
139
154
config : ValidationSchemaPluginConfig ,
140
155
tsVisitor : TsVisitor ,
0 commit comments