@@ -18,6 +18,7 @@ import {
18
18
isNamedType ,
19
19
isNonNullType ,
20
20
} from './../graphql' ;
21
+ import { buildApi , buildApiForValibot , formatDirectiveConfig } from 'src/directive' ;
21
22
22
23
export class ValibotSchemaVisitor extends BaseSchemaVisitor {
23
24
constructor ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) {
@@ -118,28 +119,39 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
118
119
if ( isListType ( parentType ) )
119
120
return `v.nullable(${ gen } )` ;
120
121
122
+ let appliedDirectivesGen = applyDirectives ( config , field , gen ) ;
123
+
121
124
if ( field . kind === Kind . INPUT_VALUE_DEFINITION ) {
122
125
const { defaultValue } = field ;
123
126
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 } )` ;
125
128
126
129
if ( defaultValue ?. kind === Kind . STRING || defaultValue ?. kind === Kind . ENUM )
127
- gen = `v.optional(${ gen } , "${ defaultValue . value } ")` ;
130
+ appliedDirectivesGen = `v.optional(${ appliedDirectivesGen } , "${ defaultValue . value } ")` ;
128
131
129
132
}
130
133
if ( isNonNullType ( parentType ) ) {
131
134
if ( visitor . shouldEmitAsNotAllowEmptyString ( type . name . value ) )
132
135
return "v.string([v.minLength(1)])" ; // TODO
133
136
134
- return gen ;
137
+ return appliedDirectivesGen ;
135
138
}
136
139
137
- return `v.nullish(${ gen } )` ;
140
+ return `v.nullish(${ appliedDirectivesGen } )` ;
138
141
}
139
142
console . warn ( 'unhandled type:' , type ) ;
140
143
return '' ;
141
144
}
142
145
146
+ function applyDirectives ( config : ValidationSchemaPluginConfig , field : InputValueDefinitionNode | FieldDefinitionNode , gen : string ) : string {
147
+ if ( config . directives && field . directives ) {
148
+ const formatted = formatDirectiveConfig ( config . directives ) ;
149
+ // TODO: handle other scalar
150
+ return `v.string([${ buildApiForValibot ( formatted , field . directives ) . join ( '' ) } ])` ;
151
+ }
152
+ return gen ;
153
+ }
154
+
143
155
function generateNameNodeValibotSchema ( config : ValidationSchemaPluginConfig , visitor : Visitor , node : NameNode ) : string {
144
156
const converter = visitor . getNameNodeConverter ( node ) ;
145
157
0 commit comments