1- import {
2- isInput ,
3- isNonNullType ,
4- isListType ,
5- isNamedType ,
6- } from "./../graphql" ;
1+ import { isInput , isNonNullType , isListType , isNamedType } from "./../graphql" ;
72import {
83 ValidationSchemaPluginConfig ,
94 ValidationSchemaVisitor ,
@@ -40,46 +35,52 @@ export const YupSchemaVisitor = (
4035 }
4136 return [ importYup ] ;
4237 } ,
43- InputObjectTypeDefinition : ( node ) => {
44- const name = node . name . value ;
45- importTypes . push ( name ) ;
46-
47- const shape = node . fields
48- ?. map ( ( field ) => generateInputObjectFieldYupSchema ( tsVisitor , schema , field , 2 ) )
49- . join ( ",\n" ) ;
50-
51- return new DeclarationBlock ( { } )
52- . export ( )
53- . asKind ( "function" )
54- . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
55- . withBlock (
56- [ indent ( `return yup.object({` ) , shape , indent ( "})" ) ] . join ( "\n" )
57- ) . string ;
58- } ,
59- EnumTypeDefinition : ( node ) => {
60- const enumname = node . name . value ;
61- importTypes . push ( enumname ) ;
38+ InputObjectTypeDefinition : {
39+ leave ( node ) {
40+ const name = node . name . value ;
41+ importTypes . push ( name ) ;
42+
43+ const shape = node . fields
44+ ?. map ( ( field ) =>
45+ generateInputObjectFieldYupSchema ( tsVisitor , schema , field , 2 )
46+ )
47+ . join ( ",\n" ) ;
6248
63- if ( config . enumsAsTypes ) {
49+ return new DeclarationBlock ( { } )
50+ . export ( )
51+ . asKind ( "function" )
52+ . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
53+ . withBlock (
54+ [ indent ( `return yup.object({` ) , shape , indent ( "})" ) ] . join ( "\n" )
55+ ) . string ;
56+ } ,
57+ } ,
58+ EnumTypeDefinition : {
59+ leave ( node ) {
60+ const enumname = node . name . value ;
61+ importTypes . push ( enumname ) ;
62+
63+ if ( config . enumsAsTypes ) {
64+ return new DeclarationBlock ( { } )
65+ . export ( )
66+ . asKind ( "const" )
67+ . withName ( `${ enumname } Schema` )
68+ . withContent (
69+ `yup.mixed().oneOf([${ node . values
70+ ?. map ( ( v ) => `'${ tsVisitor . convertName ( v . name . value ) } '` )
71+ . join ( ", " ) } ])`
72+ ) . string ;
73+ }
74+
75+ const values = node . values
76+ ?. map ( ( v ) => `${ enumname } .${ tsVisitor . convertName ( v . name . value ) } ` )
77+ . join ( ", " ) ;
6478 return new DeclarationBlock ( { } )
6579 . export ( )
6680 . asKind ( "const" )
6781 . withName ( `${ enumname } Schema` )
68- . withContent (
69- `yup.mixed().oneOf([${ node . values
70- ?. map ( ( v ) => `'${ tsVisitor . convertName ( v . name . value ) } '` )
71- . join ( ", " ) } ])`
72- ) . string ;
73- }
74-
75- const values = node . values
76- ?. map ( ( v ) => `${ enumname } .${ tsVisitor . convertName ( v . name . value ) } ` )
77- . join ( ", " ) ;
78- return new DeclarationBlock ( { } )
79- . export ( )
80- . asKind ( "const" )
81- . withName ( `${ enumname } Schema` )
82- . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
82+ . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
83+ } ,
8384 } ,
8485 // ScalarTypeDefinition: (node) => {
8586 // const decl = new DeclarationBlock({})
@@ -110,7 +111,11 @@ const generateInputObjectFieldYupSchema = (
110111 indentCount : number
111112) : string => {
112113 // TOOD(codehex): handle directive
113- const gen = generateInputObjectFieldTypeYupSchema ( tsVisitor , schema , field . type ) ;
114+ const gen = generateInputObjectFieldTypeYupSchema (
115+ tsVisitor ,
116+ schema ,
117+ field . type
118+ ) ;
114119 return indent (
115120 `${ field . name . value } : ${ maybeLazy ( field . type , gen ) } ` ,
116121 indentCount
@@ -123,11 +128,19 @@ const generateInputObjectFieldTypeYupSchema = (
123128 type : TypeNode
124129) : string => {
125130 if ( isListType ( type ) ) {
126- const gen = generateInputObjectFieldTypeYupSchema ( tsVisitor , schema , type . type ) ;
131+ const gen = generateInputObjectFieldTypeYupSchema (
132+ tsVisitor ,
133+ schema ,
134+ type . type
135+ ) ;
127136 return `yup.array().of(${ maybeLazy ( type . type , gen ) } )` ;
128137 }
129138 if ( isNonNullType ( type ) ) {
130- const gen = generateInputObjectFieldTypeYupSchema ( tsVisitor , schema , type . type ) ;
139+ const gen = generateInputObjectFieldTypeYupSchema (
140+ tsVisitor ,
141+ schema ,
142+ type . type
143+ ) ;
131144 return maybeLazy ( type . type , `${ gen } .required()` ) ;
132145 }
133146 if ( isNamedType ( type ) ) {
@@ -169,12 +182,12 @@ const yup4Scalar = (tsVisitor: TsVisitor, scalarName: string): string => {
169182 const tsType = tsVisitor . scalars [ scalarName ] ;
170183 switch ( tsType ) {
171184 case "string" :
172- return `yup.string()`
185+ return `yup.string()` ;
173186 case "number" :
174- return `yup.number()`
187+ return `yup.number()` ;
175188 case "boolean" :
176- return `yup.boolean()`
189+ return `yup.boolean()` ;
177190 }
178191 console . warn ( "unhandled name:" , scalarName ) ;
179- return `yup.mixed()`
192+ return `yup.mixed()` ;
180193} ;
0 commit comments