Skip to content

Generate Zod v4 metadata from TypeScript JSDoc comments #359

@schplitt

Description

@schplitt

Feature description

Add support for Zod v4 registries and JSDoc annotations to generate schema metadata (descriptions and examples).

With Zod v4's new registry system and the .metadata() API, it would be valuable to automatically generate schema metadata from JSDoc comments. This would allow developers to maintain a single source of truth in their TypeScript types while automatically generating Zod schemas with rich metadata for documentation, validation error messages, and OpenAPI generation.

Input

/**
 * User information
 */
interface User {
  /**
   * The user's unique identifier
   * @example "123e4567-e89b-12d3-a456-426614174000"
   */
  id: string;
  
  /**
   * The user's email address
   * @example "user@example.com"
   */
  email: string;
  
  /**
   * The user's age in years
   * @example 25
   */
  age: number;
  
  /**
   * User's role in the system
   * @example "admin"
   */
  role: "admin" | "user" | "guest";
}

Output

import { z } from "zod";

export const userSchema = z.object({
  id: z.string().metadata({
    description: "The user's unique identifier",
    examples: ["123e4567-e89b-12d3-a456-426614174000"]
  }),
  email: z.string().metadata({
    description: "The user's email address",
    examples: ["user@example.com"]
  }),
  age: z.number().metadata({
    description: "The user's age in years",
    examples: [25]
  }),
  role: z.enum(["admin", "user", "guest"]).metadata({
    description: "User's role in the system",
    examples: ["admin"]
  })
}).metadata({
  description: "User information"
});

Additional Context

  • Zod v4 metadata API: https://zod.dev/metadata
  • This would be particularly useful for generating OpenAPI schemas or providing better validation error messages
  • Multiple @example tags should result in an array of examples
  • Consider supporting other JSDoc tags like @deprecated, @see, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions