11import { isInput , isNonNullType , isListType , isNamedType } from "./../graphql" ;
2- import {
3- ValidationSchemaPluginConfig ,
4- ValidationSchemaVisitor ,
5- } from "./../types" ;
2+ import { ValidationSchemaPluginConfig } from "./../types" ;
63import {
74 InputValueDefinitionNode ,
85 NameNode ,
96 TypeNode ,
107 GraphQLSchema ,
8+ InputObjectTypeDefinitionNode ,
9+ EnumTypeDefinitionNode ,
1110} from "graphql" ;
1211import {
1312 DeclarationBlock ,
@@ -20,7 +19,7 @@ const importYup = `import * as yup from 'yup'`;
2019export const YupSchemaVisitor = (
2120 schema : GraphQLSchema ,
2221 config : ValidationSchemaPluginConfig
23- ) : ValidationSchemaVisitor => {
22+ ) => {
2423 const tsVisitor = new TsVisitor ( schema , config ) ;
2524
2625 const importTypes : string [ ] = [ ] ;
@@ -35,52 +34,48 @@ export const YupSchemaVisitor = (
3534 }
3635 return [ importYup ] ;
3736 } ,
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 ;
5754 } ,
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 ) {
7860 return new DeclarationBlock ( { } )
7961 . export ( )
8062 . asKind ( "const" )
8163 . 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 ;
8479 } ,
8580 // ScalarTypeDefinition: (node) => {
8681 // const decl = new DeclarationBlock({})
0 commit comments