@@ -32,6 +32,7 @@ import {
3232 schemaDepthVariable ,
3333 unionLiterals ,
3434 withDescription ,
35+ withTypeDescription ,
3536} from '../zod_shared.js' ;
3637
3738export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
@@ -87,9 +88,9 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
8788 const name = visitor . convertName ( node . name . value ) ;
8889 this . importTypes . push ( name ) ;
8990 if ( isOneOfInputObject ( node ) )
90- return this . buildOneOfInputFields ( node . fields ?? [ ] , visitor , name ) ;
91+ return this . buildOneOfInputFields ( node . fields ?? [ ] , visitor , name , node . description ?. value ) ;
9192
92- return this . buildInputFields ( node . fields ?? [ ] , visitor , name ) ;
93+ return this . buildInputFields ( node . fields ?? [ ] , visitor , name , node . description ?. value ) ;
9394 } ,
9495 } ;
9596 }
@@ -116,7 +117,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
116117 . export ( )
117118 . asKind ( 'const' )
118119 . withName ( `${ name } Schema: z.ZodObject<Properties<${ typeName } >>` )
119- . withContent ( buildObjectExpression ( this . config , shape ) )
120+ . withContent ( buildObjectExpression ( this . config , shape , node . description ?. value ) )
120121 . string + appendArguments
121122 ) ;
122123
@@ -127,7 +128,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
127128 . export ( )
128129 . asKind ( 'function' )
129130 . withName ( `${ name } Schema(${ schemaDepthParameter ( this . config ) } ): z.ZodObject<Properties<${ typeName } >>` )
130- . withBlock ( buildObjectReturn ( this . config , shape ) )
131+ . withBlock ( buildObjectReturn ( this . config , shape , node . description ?. value ) )
131132 . string + appendArguments
132133 ) ;
133134 }
@@ -157,7 +158,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
157158 . export ( )
158159 . asKind ( 'const' )
159160 . withName ( `${ name } Schema: z.ZodObject<Properties<${ typeName } >>` )
160- . withContent ( buildObjectExpression ( this . config , [ indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) , shape ] . join ( '\n' ) ) )
161+ . withContent ( buildObjectExpression ( this . config , [ indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) , shape ] . join ( '\n' ) , node . description ?. value ) )
161162 . string + appendArguments
162163 ) ;
163164
@@ -168,7 +169,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
168169 . export ( )
169170 . asKind ( 'function' )
170171 . withName ( `${ name } Schema(${ schemaDepthParameter ( this . config ) } ): z.ZodObject<Properties<${ typeName } >>` )
171- . withBlock ( buildObjectReturn ( this . config , [ indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) , shape ] . join ( '\n' ) ) )
172+ . withBlock ( buildObjectReturn ( this . config , [ indent ( `__typename: z.literal('${ node . name . value } ').optional(),` , 2 ) , shape ] . join ( '\n' ) , node . description ?. value ) )
172173 . string + appendArguments
173174 ) ;
174175 }
@@ -254,10 +255,11 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
254255 fields : readonly ( FieldDefinitionNode | InputValueDefinitionNode ) [ ] ,
255256 visitor : Visitor ,
256257 name : string ,
258+ description ?: string ,
257259 ) {
258260 const typeName = visitor . prefixTypeNamespace ( name ) ;
259261 const shape = fields . map ( field => generateFieldZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
260- const objectSchema = buildObjectExpression ( this . config , shape ) ;
262+ const objectSchema = buildObjectExpression ( this . config , shape , description ) ;
261263 const schemaType = hasDefaultValue ( fields ) ? `z.ZodType<${ typeName } >` : `z.ZodObject<Properties<${ typeName } >>` ;
262264
263265 switch ( this . config . validationSchemaExportType ) {
@@ -275,7 +277,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
275277 . export ( )
276278 . asKind ( 'function' )
277279 . withName ( `${ name } Schema(): ${ schemaType } ` )
278- . withBlock ( buildObjectReturn ( this . config , shape ) )
280+ . withBlock ( buildObjectReturn ( this . config , shape , description ) )
279281 . string ;
280282 }
281283 }
@@ -284,6 +286,7 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
284286 fields : readonly InputValueDefinitionNode [ ] ,
285287 visitor : Visitor ,
286288 name : string ,
289+ description ?: string ,
287290 ) {
288291 const typeName = visitor . prefixTypeNamespace ( name ) ;
289292 const variants = fields . map ( ( selectedField ) => {
@@ -298,7 +301,11 @@ export class ZodV4SchemaVisitor extends BaseSchemaVisitor {
298301
299302 return buildObjectExpression ( this . config , shape ) ;
300303 } ) ;
301- const schema = variants . length > 1 ? `z.union([\n${ variants . map ( variant => indent ( variant , 2 ) ) . join ( ',\n' ) } \n])` : variants [ 0 ] ;
304+ const schema = withTypeDescription (
305+ this . config ,
306+ description ,
307+ variants . length > 1 ? `z.union([\n${ variants . map ( variant => indent ( variant , 2 ) ) . join ( ',\n' ) } \n])` : variants [ 0 ] ,
308+ ) ;
302309
303310 switch ( this . config . validationSchemaExportType ) {
304311 case 'const' :
0 commit comments