@@ -119,36 +119,45 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
119119 if ( isListType ( parentType ) )
120120 return `v.nullable(${ gen } )` ;
121121
122- let appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
123-
124122 if ( field . kind === Kind . INPUT_VALUE_DEFINITION ) {
125123 const { defaultValue } = field ;
126124 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 } )` ;
128126
129127 if ( defaultValue ?. kind === Kind . STRING || defaultValue ?. kind === Kind . ENUM )
130- appliedDirectivesGen = `v.optional(${ appliedDirectivesGen } , "${ defaultValue . value } ")` ;
131-
128+ gen = `v.optional(${ gen } , "${ defaultValue . value } ")` ;
132129 }
130+
131+ const actions = actionsFromDirectives ( config , field ) ;
132+
133133 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+ }
136137
137- return appliedDirectivesGen ;
138+ return pipeSchemaAndActions ( gen , actions ) ;
138139 }
139140
140- return `v.nullish(${ appliedDirectivesGen } )` ;
141+ return `v.nullish(${ pipeSchemaAndActions ( gen , actions ) } )` ;
141142 }
142143 console . warn ( 'unhandled type:' , type ) ;
143144 return '' ;
144145}
145146
146- function applyDirectives ( config : ValidationSchemaPluginConfig , field : InputValueDefinitionNode | FieldDefinitionNode , gen : string ) : string {
147+ function actionsFromDirectives ( config : ValidationSchemaPluginConfig , field : InputValueDefinitionNode | FieldDefinitionNode ) : string [ ] {
147148 if ( config . directives && field . directives ) {
148149 const formatted = formatDirectiveConfig ( config . directives ) ;
149- return `v.pipe( ${ gen } , ${ buildApiForValibot ( formatted , field . directives ) . join ( ', ' ) } )` ;
150+ return buildApiForValibot ( formatted , field . directives ) ;
150151 }
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 ( ', ' ) } )` ;
152161}
153162
154163function generateNameNodeValibotSchema ( config : ValidationSchemaPluginConfig , visitor : Visitor , node : NameNode ) : string {
0 commit comments