@@ -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
2331const anySchema = `definedNonNullAnySchema` ;
2432
@@ -69,6 +77,44 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
6977 } ;
7078 }
7179
80+ get InterfaceTypeDefinition ( ) {
81+ return {
82+ leave : InterfaceTypeDefinitionBuilder ( this . config . withInterfaceType , ( node : InterfaceTypeDefinitionNode ) => {
83+ const visitor = this . createVisitor ( 'output' ) ;
84+ const name = visitor . convertName ( node . name . value ) ;
85+ this . importTypes . push ( name ) ;
86+
87+ // Building schema for field arguments.
88+ const argumentBlocks = this . buildTypeDefinitionArguments ( node , visitor ) ;
89+ const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
90+
91+ // Building schema for fields.
92+ const shape = node . fields ?. map ( field => generateFieldZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
93+
94+ switch ( this . config . validationSchemaExportType ) {
95+ case 'const' :
96+ return (
97+ new DeclarationBlock ( { } )
98+ . export ( )
99+ . asKind ( 'const' )
100+ . withName ( `${ name } Schema: z.ZodObject<Properties<${ name } >>` )
101+ . withContent ( [ `z.object({` , shape , '})' ] . join ( '\n' ) ) . string + appendArguments
102+ ) ;
103+
104+ case 'function' :
105+ default :
106+ return (
107+ new DeclarationBlock ( { } )
108+ . export ( )
109+ . asKind ( 'function' )
110+ . withName ( `${ name } Schema(): z.ZodObject<Properties<${ name } >>` )
111+ . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string + appendArguments
112+ ) ;
113+ }
114+ } ) ,
115+ } ;
116+ }
117+
72118 get ObjectTypeDefinition ( ) {
73119 return {
74120 leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
0 commit comments