1
- import {
2
- isInput ,
3
- isNonNullType ,
4
- isListType ,
5
- isNamedType ,
6
- } from "./../graphql" ;
1
+ import { isInput , isNonNullType , isListType , isNamedType } from "./../graphql" ;
7
2
import {
8
3
ValidationSchemaPluginConfig ,
9
4
ValidationSchemaVisitor ,
@@ -40,46 +35,52 @@ export const YupSchemaVisitor = (
40
35
}
41
36
return [ importYup ] ;
42
37
} ,
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" ) ;
62
48
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 ( ", " ) ;
64
78
return new DeclarationBlock ( { } )
65
79
. export ( )
66
80
. asKind ( "const" )
67
81
. 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
+ } ,
83
84
} ,
84
85
// ScalarTypeDefinition: (node) => {
85
86
// const decl = new DeclarationBlock({})
@@ -110,7 +111,11 @@ const generateInputObjectFieldYupSchema = (
110
111
indentCount : number
111
112
) : string => {
112
113
// TOOD(codehex): handle directive
113
- const gen = generateInputObjectFieldTypeYupSchema ( tsVisitor , schema , field . type ) ;
114
+ const gen = generateInputObjectFieldTypeYupSchema (
115
+ tsVisitor ,
116
+ schema ,
117
+ field . type
118
+ ) ;
114
119
return indent (
115
120
`${ field . name . value } : ${ maybeLazy ( field . type , gen ) } ` ,
116
121
indentCount
@@ -123,11 +128,19 @@ const generateInputObjectFieldTypeYupSchema = (
123
128
type : TypeNode
124
129
) : string => {
125
130
if ( isListType ( type ) ) {
126
- const gen = generateInputObjectFieldTypeYupSchema ( tsVisitor , schema , type . type ) ;
131
+ const gen = generateInputObjectFieldTypeYupSchema (
132
+ tsVisitor ,
133
+ schema ,
134
+ type . type
135
+ ) ;
127
136
return `yup.array().of(${ maybeLazy ( type . type , gen ) } )` ;
128
137
}
129
138
if ( isNonNullType ( type ) ) {
130
- const gen = generateInputObjectFieldTypeYupSchema ( tsVisitor , schema , type . type ) ;
139
+ const gen = generateInputObjectFieldTypeYupSchema (
140
+ tsVisitor ,
141
+ schema ,
142
+ type . type
143
+ ) ;
131
144
return maybeLazy ( type . type , `${ gen } .required()` ) ;
132
145
}
133
146
if ( isNamedType ( type ) ) {
@@ -169,12 +182,12 @@ const yup4Scalar = (tsVisitor: TsVisitor, scalarName: string): string => {
169
182
const tsType = tsVisitor . scalars [ scalarName ] ;
170
183
switch ( tsType ) {
171
184
case "string" :
172
- return `yup.string()`
185
+ return `yup.string()` ;
173
186
case "number" :
174
- return `yup.number()`
187
+ return `yup.number()` ;
175
188
case "boolean" :
176
- return `yup.boolean()`
189
+ return `yup.boolean()` ;
177
190
}
178
191
console . warn ( "unhandled name:" , scalarName ) ;
179
- return `yup.mixed()`
192
+ return `yup.mixed()` ;
180
193
} ;
0 commit comments