agentleFS
NembieNembie/claude-code-skills/skills/zod-schema-generator/SKILL.md

zod-schema-generator

Skill3 starsChanged 9 months agoMITView on GitHub

Generate Zod validation schemas from Prisma models, TypeScript interfaces, or JSON examples. Use when asked to generate Zod schemas, create validation, bridge Prisma to Zod, validate API input/output, or generate runtime type checks.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Before generating any output, read config/defaults.md and adapt all patterns, imports, and code examples to the user's configured stack.

    line 8
  2. Very specific:

    Add .transform() and .refine() where appropriate.

    line 15
  3. Very specific:

    Colocated: Place schema next to the model/route it validates (e.g., app/api/users/schema.ts).

    line 203
  4. Very specific:

    Centralized: Place in lib/validations/ or src/schemas/ when schemas are shared across multiple routes.

    line 204

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Zod Schema Generator

Before generating any output, read config/defaults.md and adapt all patterns, imports, and code examples to the user's configured stack.

Process

  1. Identify the source: Prisma model, TypeScript interface/type, or raw JSON example.
  2. Determine schema purpose: input validation (create/update), output validation (API response), or full round-trip.
  3. Generate Zod schema with correct types, optional fields, and refinements.
  4. Add .transform() and .refine() where appropriate.
  5. Output the schema file with proper imports and exports.

Source: Prisma Model

Parse the Prisma model and generate both input and output schemas.

Input Schema (for create/update)

Omit auto-generated fields (id, createdAt, updatedAt) and fields with @default.

model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  posts     Post[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
// Generated: user.schema.ts
import { z } from "zod";

export const createUserSchema = z.object({
  email: z.string().email().trim().toLowerCase(),
  name: z.string().min(1).trim().optional(),
  role: z.enum(["USER", "ADMIN"]).optional(), // has @default
});

export const updateUserSchema = createUserSchema.partial();

export type CreateUserInput = z.infer<typeof createUserSchema>;
export type UpdateUserInput = z.infer<typeof updateUserSchema>;

Output Schema (for API responses)

Include all fields. Use z.coerce.date() for DateTime fields.

export const userSchema = z.object({
  id: z.string().cuid(),
  email: z.string().email(),
  name: z.string().nullable(),
  role: z.enum(["USER", "ADMIN"]),
  createdAt: z.coerce.date(),
  updatedAt: z.coerce.date(),
});

export type User = z.infer<typeof userSchema>;

Relations

For input schemas, generate nested create/connect patterns:

// One-to-many: creating a post with author
export const createPostSchema = z.object({
  title: z.string().min(1).max(255),
  content: z.string(),
  author: z.union([
    z.object({ connect: z.object({ id: z.string() }) }),
    z.object({ create: createUserSchema }),
  ]),
});

For output schemas, include the related object shape:

export const postWithAuthorSchema = postSchema.extend({
  author: userSchema,
});

Source: TypeScript Interface

Map TypeScript types directly to Zod equivalents.

// Input
interface OrderItem {
  productId: string;
  quantity: number;
  price: number;
  notes?: string;
  metadata: Record<string, unknown>;
}
// Generated
export const orderItemSchema = z.object({
  productId: z.string().uuid(),
  quantity: z.number().int().positive(),
  price: z.number().nonnegative(),
  notes: z.string().optional(),
  metadata: z.record(z.string(), z.unknown()),
});

Discriminated Unions

// Input
type PaymentMethod =
  | { type: "card"; cardNumber: string; expiry: string }
  | { type: "bank"; accountNumber: string; routingNumber: string }
  | { type: "crypto"; walletAddress: string };
// Generated
export const paymentMethodSchema = z.discriminatedUnion("type", [
  z.object({
    type: z.literal("card"),
    cardNumber: z.string().regex(/^\d{16}$/),
    expiry: z.string().regex(/^\d{2}\/\d{2}$/),
  }),
  z.object({
    type: z.literal("bank"),
    accountNumber: z.string(),
    routingNumber: z.string().regex(/^\d{9}$/),
  }),
  z.object({
    type: z.literal("crypto"),
    walletAddress: z.string().min(26).max(62),
  }),
]);

Source: JSON Example

Infer schema from a JSON payload by analyzing value types and structure.

{
  "name": "Acme Corp",
  "employees": 150,
  "active": true,
  "tags": ["tech", "startup"],
  "address": {
    "street": "123 Main St",
    "city": "Springfield",
    "zip": "62701"
  }
}
// Generated (with refinement suggestions in comments)
export const companySchema = z.object({
  name: z.string().min(1),
  employees: z.number().int().nonnegative(),
  active: z.boolean(),
  tags: z.array(z.string()),
  address: z.object({
    street: z.string(),
    city: z.string(),
    zip: z.string().regex(/^\d{5}(-\d{4})?$/), // inferred US zip
  }),
});

Common Refinements

Apply these automatically when field names or patterns suggest them:

Field patternRefinement
email.email().trim().toLowerCase()
url, website.url()
id, *Id (cuid).cuid() or .cuid2()
uuid, *Uuid.uuid()
phone.regex(/^\+?[\d\s-()]+$/)
password.min(8)
*At (timestamps)z.coerce.date()
slug.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)
price, amount.nonnegative()
quantity, count.int().nonnegative()

File Placement

  • Colocated: Place schema next to the model/route it validates (e.g., app/api/users/schema.ts).
  • Centralized: Place in lib/validations/ or src/schemas/ when schemas are shared across multiple routes.

Match the project's existing pattern. Default to colocated if no convention exists.

Output Format

## Generated Zod Schema

**Source**: [Prisma model | TypeScript interface | JSON example]
**File**: `path/to/schema.ts`

[Generated code block]

### Refinements Applied
- `email`: Added `.email().trim().toLowerCase()`
- `createdAt`: Used `z.coerce.date()` for DateTime

### Usage Example
[Short example showing schema.parse() or schema.safeParse()]

Reference

See references/prisma-zod-mapping.md for the complete Prisma-to-Zod type mapping table and refinement catalog.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.