@@ -18,6 +18,7 @@ import {
1818 isNamedType ,
1919 isNonNullType ,
2020} from './../graphql' ;
21+ import { buildApiForValibot , formatDirectiveConfig } from 'src/directive' ;
2122
2223export class ValibotSchemaVisitor extends BaseSchemaVisitor {
2324 constructor ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) {
@@ -118,28 +119,38 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
118119 if ( isListType ( parentType ) )
119120 return `v.nullable(${ gen } )` ;
120121
122+ let appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
123+
121124 if ( field . kind === Kind . INPUT_VALUE_DEFINITION ) {
122125 const { defaultValue } = field ;
123126 if ( defaultValue ?. kind === Kind . INT || defaultValue ?. kind === Kind . FLOAT || defaultValue ?. kind === Kind . BOOLEAN )
124- gen = `v.optional(${ gen } , ${ defaultValue . value } )` ;
127+ appliedDirectivesGen = `v.optional(${ appliedDirectivesGen } , ${ defaultValue . value } )` ;
125128
126129 if ( defaultValue ?. kind === Kind . STRING || defaultValue ?. kind === Kind . ENUM )
127- gen = `v.optional(${ gen } , "${ defaultValue . value } ")` ;
130+ appliedDirectivesGen = `v.optional(${ appliedDirectivesGen } , "${ defaultValue . value } ")` ;
128131
129132 }
130133 if ( isNonNullType ( parentType ) ) {
131134 if ( visitor . shouldEmitAsNotAllowEmptyString ( type . name . value ) )
132135 return "v.string([v.minLength(1)])" ; // TODO
133136
134- return gen ;
137+ return appliedDirectivesGen ;
135138 }
136139
137- return `v.nullish(${ gen } )` ;
140+ return `v.nullish(${ appliedDirectivesGen } )` ;
138141 }
139142 console . warn ( 'unhandled type:' , type ) ;
140143 return '' ;
141144}
142145
146+ function applyDirectives ( config : ValidationSchemaPluginConfig , field : InputValueDefinitionNode | FieldDefinitionNode , gen : string ) : string {
147+ if ( config . directives && field . directives ) {
148+ const formatted = formatDirectiveConfig ( config . directives ) ;
149+ return `v.pipe(${ gen } , ${ buildApiForValibot ( formatted , field . directives ) . join ( ', ' ) } )` ;
150+ }
151+ return gen ;
152+ }
153+
143154function generateNameNodeValibotSchema ( config : ValidationSchemaPluginConfig , visitor : Visitor , node : NameNode ) : string {
144155 const converter = visitor . getNameNodeConverter ( node ) ;
145156
0 commit comments