@@ -23,6 +23,7 @@ import {
2323import { buildApi , formatDirectiveConfig } from '../directive.js' ;
2424import {
2525 escapeGraphQLCharacters ,
26+ escapeForDescribe ,
2627 InterfaceTypeDefinitionBuilder ,
2728 isListType ,
2829 isNamedType ,
@@ -78,7 +79,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
7879 const visitor = this . createVisitor ( 'input' ) ;
7980 const name = visitor . convertName ( node . name . value ) ;
8081 this . importTypes . push ( name ) ;
81- return this . buildInputFields ( node . fields ?? [ ] , visitor , name ) ;
82+ return this . buildInputFields ( node . fields ?? [ ] , visitor , name , node . description ?. value ) ;
8283 } ,
8384 } ;
8485 }
@@ -98,14 +99,18 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
9899 // Building schema for fields.
99100 const shape = node . fields ?. map ( field => generateFieldZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
100101
102+ const maybeDescribe = this . config . withDescriptions && node . description ?. value
103+ ? `.describe('${ escapeForDescribe ( node . description . value ) } ')`
104+ : '' ;
105+
101106 switch ( this . config . validationSchemaExportType ) {
102107 case 'const' :
103108 return (
104109 new DeclarationBlock ( { } )
105110 . export ( )
106111 . asKind ( 'const' )
107112 . withName ( `${ name } Schema: z.ZodObject<Properties<${ typeName } >>` )
108- . withContent ( [ `z.object({` , shape , '})' ] . join ( '\n' ) )
113+ . withContent ( [ `z.object({` , shape , `}) ${ maybeDescribe } ` ] . join ( '\n' ) )
109114 . string + appendArguments
110115 ) ;
111116
@@ -116,7 +121,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
116121 . export ( )
117122 . asKind ( 'function' )
118123 . withName ( `${ name } Schema(): z.ZodObject<Properties<${ typeName } >>` )
119- . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) )
124+ . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( `}) ${ maybeDescribe } ` ) ] . join ( '\n' ) )
120125 . string + appendArguments
121126 ) ;
122127 }
@@ -139,6 +144,10 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
139144 // Building schema for fields.
140145 const shape = node . fields ?. map ( field => generateFieldZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
141146
147+ const maybeDescribe = this . config . withDescriptions && node . description ?. value
148+ ? `.describe('${ escapeForDescribe ( node . description . value ) } ')`
149+ : '' ;
150+
142151 switch ( this . config . validationSchemaExportType ) {
143152 case 'const' :
144153 return (
@@ -151,7 +160,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
151160 `z.object({` ,
152161 indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) ,
153162 shape ,
154- '})' ,
163+ `}) ${ maybeDescribe } ` ,
155164 ] . join ( '\n' ) ,
156165 )
157166 . string + appendArguments
@@ -169,7 +178,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
169178 indent ( `return z.object({` ) ,
170179 indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) ,
171180 shape ,
172- indent ( '})' ) ,
181+ indent ( `}) ${ maybeDescribe } ` ) ,
173182 ] . join ( '\n' ) ,
174183 )
175184 . string + appendArguments
@@ -249,17 +258,22 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
249258 fields : readonly ( FieldDefinitionNode | InputValueDefinitionNode ) [ ] ,
250259 visitor : Visitor ,
251260 name : string ,
261+ description ?: string ,
252262 ) {
253263 const typeName = visitor . prefixTypeNamespace ( name ) ;
254264 const shape = fields . map ( field => generateFieldZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
255265
266+ const maybeDescribe = this . config . withDescriptions && description
267+ ? `.describe('${ escapeForDescribe ( description ) } ')`
268+ : '' ;
269+
256270 switch ( this . config . validationSchemaExportType ) {
257271 case 'const' :
258272 return new DeclarationBlock ( { } )
259273 . export ( )
260274 . asKind ( 'const' )
261275 . withName ( `${ name } Schema: z.ZodObject<Properties<${ typeName } >>` )
262- . withContent ( [ ' z.object({' , shape , '})' ] . join ( '\n' ) )
276+ . withContent ( [ ` z.object({` , shape , `}) ${ maybeDescribe } ` ] . join ( '\n' ) )
263277 . string ;
264278
265279 case 'function' :
@@ -268,15 +282,19 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
268282 . export ( )
269283 . asKind ( 'function' )
270284 . withName ( `${ name } Schema(): z.ZodObject<Properties<${ typeName } >>` )
271- . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) )
285+ . withBlock ( [ indent ( `return z.object({` ) , shape , indent ( `}) ${ maybeDescribe } ` ) ] . join ( '\n' ) )
272286 . string ;
273287 }
274288 }
275289}
276290
277291function generateFieldZodSchema ( config : ValidationSchemaPluginConfig , visitor : Visitor , field : InputValueDefinitionNode | FieldDefinitionNode , indentCount : number ) : string {
278292 const gen = generateFieldTypeZodSchema ( config , visitor , field , field . type ) ;
279- return indent ( `${ field . name . value } : ${ maybeLazy ( visitor , field . type , gen ) } ` , indentCount ) ;
293+ let schema = maybeLazy ( visitor , field . type , gen ) ;
294+ if ( config . withDescriptions && field . description ?. value ) {
295+ schema = `${ schema } .describe('${ escapeForDescribe ( field . description . value ) } ')` ;
296+ }
297+ return indent ( `${ field . name . value } : ${ schema } ` , indentCount ) ;
280298}
281299
282300function generateFieldTypeZodSchema ( config : ValidationSchemaPluginConfig , visitor : Visitor , field : InputValueDefinitionNode | FieldDefinitionNode , type : TypeNode , parentType ?: TypeNode ) : string {
0 commit comments