@@ -30,6 +30,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
3030 return [ importYup ] ;
3131 } ,
3232 initialEmit : ( ) : string =>
33+ '\n' +
3334 new DeclarationBlock ( { } )
3435 . asKind ( 'function' )
3536 . withName ( 'union<T>(...schemas: ReadonlyArray<yup.SchemaOf<T>>): yup.BaseSchema<T>' )
@@ -101,22 +102,21 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
101102 . withContent ( `yup.mixed().oneOf([${ values } ])` ) . string ;
102103 } ,
103104 UnionTypeDefinition : ( node : UnionTypeDefinitionNode ) => {
105+ if ( ! node . types ) return ;
106+
104107 const unionName = tsVisitor . convertName ( node . name . value ) ;
108+ importTypes . push ( unionName ) ;
109+
105110 const unionElements = node . types ?. map ( t => `${ tsVisitor . convertName ( t . name . value ) } Schema()` ) . join ( ', ' ) ;
106111 const unionElementsCount = node . types ?. length ?? 0 ;
107112
108- const union =
109- unionElementsCount > 1
110- ? indent ( `return union<${ unionName } >(${ unionElements } )` )
111- : indent ( `return ${ unionElements } ` ) ;
113+ const union = unionElementsCount > 1 ? `union<${ unionName } >(${ unionElements } )` : unionElements ;
112114
113- const result = new DeclarationBlock ( { } )
115+ return new DeclarationBlock ( { } )
114116 . export ( )
115- . asKind ( 'function' )
116- . withName ( `${ unionName } Schema(): yup.BaseSchema<${ unionName } >` )
117- . withBlock ( union ) ;
118-
119- return result . string ;
117+ . asKind ( 'const' )
118+ . withName ( `${ unionName } Schema: yup.BaseSchema<${ unionName } >` )
119+ . withContent ( union ) . string ;
120120 } ,
121121 // ScalarTypeDefinition: (node) => {
122122 // const decl = new DeclarationBlock({})
@@ -209,6 +209,11 @@ const generateNameNodeYupSchema = (
209209 return `${ enumName } Schema` ;
210210 }
211211
212+ if ( typ ?. astNode ?. kind === 'UnionTypeDefinition' ) {
213+ const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
214+ return `${ enumName } Schema` ;
215+ }
216+
212217 const primitive = yup4Scalar ( config , tsVisitor , node . value ) ;
213218 return primitive ;
214219} ;
0 commit comments