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