@@ -5,6 +5,7 @@ import type {
5
5
GraphQLSchema ,
6
6
InputObjectTypeDefinitionNode ,
7
7
InputValueDefinitionNode ,
8
+ InterfaceTypeDefinitionNode ,
8
9
NameNode ,
9
10
ObjectTypeDefinitionNode ,
10
11
TypeNode ,
@@ -17,8 +18,15 @@ import {
17
18
import type { ValidationSchemaPluginConfig } from '../config' ;
18
19
import { buildApi , formatDirectiveConfig } from '../directive' ;
19
20
import { BaseSchemaVisitor } from '../schema_visitor' ;
20
- import type { Visitor } from '../visitor' ;
21
- import { ObjectTypeDefinitionBuilder , isInput , isListType , isNamedType , isNonNullType } from './../graphql' ;
21
+ import { Visitor } from '../visitor' ;
22
+ import {
23
+ InterfaceTypeDefinitionBuilder ,
24
+ isInput ,
25
+ isListType ,
26
+ isNamedType ,
27
+ isNonNullType ,
28
+ ObjectTypeDefinitionBuilder ,
29
+ } from './../graphql' ;
22
30
23
31
export class YupSchemaVisitor extends BaseSchemaVisitor {
24
32
constructor ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) {
@@ -60,6 +68,49 @@ export class YupSchemaVisitor extends BaseSchemaVisitor {
60
68
} ;
61
69
}
62
70
71
+ get InterfaceTypeDefinition ( ) {
72
+ return {
73
+ leave : InterfaceTypeDefinitionBuilder ( this . config . withInterfaceType , ( node : InterfaceTypeDefinitionNode ) => {
74
+ const visitor = this . createVisitor ( 'output' ) ;
75
+ const name = visitor . convertName ( node . name . value ) ;
76
+ this . importTypes . push ( name ) ;
77
+
78
+ // Building schema for field arguments.
79
+ const argumentBlocks = this . buildTypeDefinitionArguments ( node , visitor ) ;
80
+ const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
81
+
82
+ // Building schema for fields.
83
+ const shape = node . fields
84
+ ?. map ( field => {
85
+ const fieldSchema = generateFieldYupSchema ( this . config , visitor , field , 2 ) ;
86
+ return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
87
+ } )
88
+ . join ( ',\n' ) ;
89
+
90
+ switch ( this . config . validationSchemaExportType ) {
91
+ case 'const' :
92
+ return (
93
+ new DeclarationBlock ( { } )
94
+ . export ( )
95
+ . asKind ( 'const' )
96
+ . withName ( `${ name } Schema: yup.ObjectSchema<${ name } >` )
97
+ . withContent ( [ `yup.object({` , shape , '})' ] . join ( '\n' ) ) . string + appendArguments
98
+ ) ;
99
+
100
+ case 'function' :
101
+ default :
102
+ return (
103
+ new DeclarationBlock ( { } )
104
+ . export ( )
105
+ . asKind ( 'function' )
106
+ . withName ( `${ name } Schema(): yup.ObjectSchema<${ name } >` )
107
+ . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string + appendArguments
108
+ ) ;
109
+ }
110
+ } ) ,
111
+ } ;
112
+ }
113
+
63
114
get ObjectTypeDefinition ( ) {
64
115
return {
65
116
leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
@@ -286,6 +337,7 @@ function generateNameNodeYupSchema(config: ValidationSchemaPluginConfig, visitor
286
337
const converter = visitor . getNameNodeConverter ( node ) ;
287
338
288
339
switch ( converter ?. targetKind ) {
340
+ case 'InterfaceTypeDefinition' :
289
341
case 'InputObjectTypeDefinition' :
290
342
case 'ObjectTypeDefinition' :
291
343
case 'UnionTypeDefinition' :
0 commit comments