@@ -5,6 +5,7 @@ import type {
55 GraphQLSchema ,
66 InputObjectTypeDefinitionNode ,
77 InputValueDefinitionNode ,
8+ InterfaceTypeDefinitionNode ,
89 NameNode ,
910 ObjectTypeDefinitionNode ,
1011 TypeNode ,
@@ -17,8 +18,15 @@ import {
1718import type { ValidationSchemaPluginConfig } from '../config' ;
1819import { buildApi , formatDirectiveConfig } from '../directive' ;
1920import { 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' ;
2230
2331export class YupSchemaVisitor extends BaseSchemaVisitor {
2432 constructor ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) {
@@ -60,6 +68,49 @@ export class YupSchemaVisitor extends BaseSchemaVisitor {
6068 } ;
6169 }
6270
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+
63114 get ObjectTypeDefinition ( ) {
64115 return {
65116 leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
@@ -286,6 +337,7 @@ function generateNameNodeYupSchema(config: ValidationSchemaPluginConfig, visitor
286337 const converter = visitor . getNameNodeConverter ( node ) ;
287338
288339 switch ( converter ?. targetKind ) {
340+ case 'InterfaceTypeDefinition' :
289341 case 'InputObjectTypeDefinition' :
290342 case 'ObjectTypeDefinition' :
291343 case 'UnionTypeDefinition' :
0 commit comments