@@ -121,36 +121,45 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
121
121
if ( isListType ( parentType ) )
122
122
return `v.nullable(${ gen } )` ;
123
123
124
- let appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
125
-
126
124
if ( field . kind === Kind . INPUT_VALUE_DEFINITION ) {
127
125
const { defaultValue } = field ;
128
126
if ( defaultValue ?. kind === Kind . INT || defaultValue ?. kind === Kind . FLOAT || defaultValue ?. kind === Kind . BOOLEAN )
129
- appliedDirectivesGen = `v.optional(${ appliedDirectivesGen } , ${ defaultValue . value } )` ;
127
+ gen = `v.optional(${ gen } , ${ defaultValue . value } )` ;
130
128
131
129
if ( defaultValue ?. kind === Kind . STRING || defaultValue ?. kind === Kind . ENUM )
132
- appliedDirectivesGen = `v.optional(${ appliedDirectivesGen } , "${ defaultValue . value } ")` ;
133
-
130
+ gen = `v.optional(${ gen } , "${ defaultValue . value } ")` ;
134
131
}
132
+
133
+ const actions = actionsFromDirectives ( config , field ) ;
134
+
135
135
if ( isNonNullType ( parentType ) ) {
136
- if ( visitor . shouldEmitAsNotAllowEmptyString ( type . name . value ) )
137
- return "v.string([v.minLength(1)])" ; // TODO
136
+ if ( visitor . shouldEmitAsNotAllowEmptyString ( type . name . value ) ) {
137
+ actions . push ( 'v.minLength(1)' ) ;
138
+ }
138
139
139
- return appliedDirectivesGen ;
140
+ return pipeSchemaAndActions ( gen , actions ) ;
140
141
}
141
142
142
- return `v.nullish(${ appliedDirectivesGen } )` ;
143
+ return `v.nullish(${ pipeSchemaAndActions ( gen , actions ) } )` ;
143
144
}
144
145
console . warn ( 'unhandled type:' , type ) ;
145
146
return '' ;
146
147
}
147
148
148
- function applyDirectives ( config : ValidationSchemaPluginConfig , field : InputValueDefinitionNode | FieldDefinitionNode , gen : string ) : string {
149
+ function actionsFromDirectives ( config : ValidationSchemaPluginConfig , field : InputValueDefinitionNode | FieldDefinitionNode ) : string [ ] {
149
150
if ( config . directives && field . directives ) {
150
151
const formatted = formatDirectiveConfig ( config . directives ) ;
151
- return `v.pipe( ${ gen } , ${ buildApiForValibot ( formatted , field . directives ) . join ( ', ' ) } )` ;
152
+ return buildApiForValibot ( formatted , field . directives ) ;
152
153
}
153
- return gen ;
154
+
155
+ return [ ] ;
156
+ }
157
+
158
+ function pipeSchemaAndActions ( schema : string , actions : string [ ] ) : string {
159
+ if ( actions . length === 0 )
160
+ return schema ;
161
+
162
+ return `v.pipe(${ schema } , ${ actions . join ( ', ' ) } )` ;
154
163
}
155
164
156
165
function generateNameNodeValibotSchema ( config : ValidationSchemaPluginConfig , visitor : Visitor , node : NameNode ) : string {
0 commit comments