1
1
import { isInput , isNonNullType , isListType , isNamedType } from "./../graphql" ;
2
- import {
3
- ValidationSchemaPluginConfig ,
4
- ValidationSchemaVisitor ,
5
- } from "./../types" ;
2
+ import { ValidationSchemaPluginConfig } from "./../types" ;
6
3
import {
7
4
InputValueDefinitionNode ,
8
5
NameNode ,
9
6
TypeNode ,
10
7
GraphQLSchema ,
8
+ InputObjectTypeDefinitionNode ,
9
+ EnumTypeDefinitionNode ,
11
10
} from "graphql" ;
12
11
import {
13
12
DeclarationBlock ,
@@ -20,7 +19,7 @@ const importYup = `import * as yup from 'yup'`;
20
19
export const YupSchemaVisitor = (
21
20
schema : GraphQLSchema ,
22
21
config : ValidationSchemaPluginConfig
23
- ) : ValidationSchemaVisitor => {
22
+ ) => {
24
23
const tsVisitor = new TsVisitor ( schema , config ) ;
25
24
26
25
const importTypes : string [ ] = [ ] ;
@@ -35,52 +34,48 @@ export const YupSchemaVisitor = (
35
34
}
36
35
return [ importYup ] ;
37
36
} ,
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" ) ;
48
-
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
- } ,
37
+ InputObjectTypeDefinition : ( node : InputObjectTypeDefinitionNode ) => {
38
+ const name = node . name . value ;
39
+ importTypes . push ( name ) ;
40
+
41
+ const shape = node . fields
42
+ ?. map ( ( field ) =>
43
+ generateInputObjectFieldYupSchema ( tsVisitor , schema , field , 2 )
44
+ )
45
+ . join ( ",\n" ) ;
46
+
47
+ return new DeclarationBlock ( { } )
48
+ . export ( )
49
+ . asKind ( "function" )
50
+ . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
51
+ . withBlock (
52
+ [ indent ( `return yup.object({` ) , shape , indent ( "})" ) ] . join ( "\n" )
53
+ ) . string ;
57
54
} ,
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 ( ", " ) ;
55
+ EnumTypeDefinition : ( node : EnumTypeDefinitionNode ) => {
56
+ const enumname = node . name . value ;
57
+ importTypes . push ( enumname ) ;
58
+
59
+ if ( config . enumsAsTypes ) {
78
60
return new DeclarationBlock ( { } )
79
61
. export ( )
80
62
. asKind ( "const" )
81
63
. withName ( `${ enumname } Schema` )
82
- . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
83
- } ,
64
+ . withContent (
65
+ `yup.mixed().oneOf([${ node . values
66
+ ?. map ( ( v ) => `'${ tsVisitor . convertName ( v . name . value ) } '` )
67
+ . join ( ", " ) } ])`
68
+ ) . string ;
69
+ }
70
+
71
+ const values = node . values
72
+ ?. map ( ( v ) => `${ enumname } .${ tsVisitor . convertName ( v . name . value ) } ` )
73
+ . join ( ", " ) ;
74
+ return new DeclarationBlock ( { } )
75
+ . export ( )
76
+ . asKind ( "const" )
77
+ . withName ( `${ enumname } Schema` )
78
+ . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
84
79
} ,
85
80
// ScalarTypeDefinition: (node) => {
86
81
// const decl = new DeclarationBlock({})
0 commit comments