Zod v4 Expert Guidance
Overview
This skill provides comprehensive guidance on using Zod v4, TypeScript's leading validation library. It covers breaking changes from v3, migration patterns, core API usage, and real-world validation patterns.
When to Use This Skill
Use this skill when:
- Writing validation schemas with Zod v4
- Migrating code from Zod v3 to v4
- Implementing form validation, API response validation, or config validation
- Working with TypeScript type inference from Zod schemas
- Debugging Zod validation errors
- Designing type-safe data models
- Creating recursive or complex nested schemas
Quick Reference
| Task |
Reference Document |
| Migrating from Zod v3 |
references/migration-from-v3.md |
| Core API and primitives |
references/core-api.md |
| Real-world patterns |
references/common-patterns.md |
Key Breaking Changes in v4
CRITICAL: Always review these when working with Zod v4:
- Error customization: Use
error parameter instead of message, invalid_type_error, required_error
- String formats: Moved to top-level functions (e.g.,
z.email() not z.string().email())
- Function schemas: Completely redesigned API using
z.function({ input, output }).implement()
- Error handling: Use
.issues instead of .errors, z.treeifyError() for formatting
- Object methods: Use
z.strictObject() and z.looseObject() instead of .strict() and .passthrough()
- Extending schemas: Use shape spreading (
z.object({ ...Base.shape, ... })) instead of .merge() or .extend()
- Records: Must specify both key and value schemas:
z.record(keySchema, valueSchema)
- Defaults in optional fields: Now apply even inside optional fields (behavioral change)
- Number validation: Infinity rejected by default,
.int() enforces safe range
See references/migration-from-v3.md for complete migration guide.
Common Patterns
Basic Validation
import { z } from 'zod/v4';
// Simple schema
const userSchema = z.object({
name: z.string().min(1),
email: z.email(),
age: z.number().int().positive(),
});
// Parse with error handling
const result = userSchema.safeParse(data);
if (!result.success) {
console.error(result.error.issues);
}
Type Inference
// Automatically infer TypeScript type from schema
type User = z.infer<typeof userSchema>;
// { name: string; email: string; age: number }
Custom Validation
const passwordSchema = z
.string()
.min(8, { error: 'Password must be at least 8 characters' })
.regex(/[A-Z]/, { error: 'Must contain uppercase letter' })
.regex(/[0-9]/, { error: 'Must contain number' });
Best Practices
- Always use
.safeParse() for user input - Never let validation errors crash your app
- Leverage type inference - Don't manually type what Zod can infer
- Use top-level format validators -
z.email() not z.string().email() (v4 pattern)
- Prefer
z.strictObject() - Catch typos and unexpected fields
- Keep refinements simple - Complex business logic should be separate
- Reuse schemas - Define once, reference everywhere
- Document complex schemas - Use TypeScript JSDoc comments
Workflow
When working with Zod:
- Define schemas - Start with the shape of your data
- Add validations - Layer on constraints (min, max, regex, etc.)
- Add custom errors - Make validation messages user-friendly
- Infer types - Use
z.infer<typeof schema> for TypeScript types
- Parse safely - Use
.safeParse() and handle errors gracefully
- Test edge cases - Validate your validation logic
Resources
- Migration Guide: references/migration-from-v3.md
- Core API Reference: references/core-api.md
- Common Patterns: references/common-patterns.md
- Official Docs: https://zod.dev/v4
Custom Instructions
Source: boneskull/claude-plugins — distributed by TomeVault.
1---2name: zod-v4-23description: Expert guidance on Zod v4 validation library including breaking changes from v3, migration patterns, core API usage, and common validation patterns. Use when working with Zod schemas, validation, type inference, or migrating from Zod v3. Use when this capability is needed.4---56# Zod v4 Expert Guidance78## Overview910This skill provides comprehensive guidance on using Zod v4, TypeScript's leading validation library. It covers breaking changes from v3, migration patterns, core API usage, and real-world validation patterns.1112## When to Use This Skill1314Use this skill when:1516- Writing validation schemas with Zod v417- Migrating code from Zod v3 to v418- Implementing form validation, API response validation, or config validation19- Working with TypeScript type inference from Zod schemas20- Debugging Zod validation errors21- Designing type-safe data models22- Creating recursive or complex nested schemas2324## Quick Reference2526| Task | Reference Document |27| ----------------------- | ------------------------------------------------------------------ |28| Migrating from Zod v3 | [references/migration-from-v3.md](references/migration-from-v3.md) |29| Core API and primitives | [references/core-api.md](references/core-api.md) |30| Real-world patterns | [references/common-patterns.md](references/common-patterns.md) |3132## Key Breaking Changes in v43334**CRITICAL:** Always review these when working with Zod v4:35361. **Error customization**: Use `error` parameter instead of `message`, `invalid_type_error`, `required_error`372. **String formats**: Moved to top-level functions (e.g., `z.email()` not `z.string().email()`)383. **Function schemas**: Completely redesigned API using `z.function({ input, output }).implement()`394. **Error handling**: Use `.issues` instead of `.errors`, `z.treeifyError()` for formatting405. **Object methods**: Use `z.strictObject()` and `z.looseObject()` instead of `.strict()` and `.passthrough()`416. **Extending schemas**: Use shape spreading (`z.object({ ...Base.shape, ... })`) instead of `.merge()` or `.extend()`427. **Records**: Must specify both key and value schemas: `z.record(keySchema, valueSchema)`438. **Defaults in optional fields**: Now apply even inside optional fields (behavioral change)449. **Number validation**: Infinity rejected by default, `.int()` enforces safe range4546See [references/migration-from-v3.md](references/migration-from-v3.md) for complete migration guide.4748## Common Patterns4950### Basic Validation5152```typescript53import { z } from 'zod/v4';5455// Simple schema56const userSchema = z.object({57 name: z.string().min(1),58 email: z.email(),59 age: z.number().int().positive(),60});6162// Parse with error handling63const result = userSchema.safeParse(data);64if (!result.success) {65 console.error(result.error.issues);66}67```6869### Type Inference7071```typescript72// Automatically infer TypeScript type from schema73type User = z.infer<typeof userSchema>;74// { name: string; email: string; age: number }75```7677### Custom Validation7879```typescript80const passwordSchema = z81 .string()82 .min(8, { error: 'Password must be at least 8 characters' })83 .regex(/[A-Z]/, { error: 'Must contain uppercase letter' })84 .regex(/[0-9]/, { error: 'Must contain number' });85```8687## Best Practices88891. **Always use `.safeParse()` for user input** - Never let validation errors crash your app902. **Leverage type inference** - Don't manually type what Zod can infer913. **Use top-level format validators** - `z.email()` not `z.string().email()` (v4 pattern)924. **Prefer `z.strictObject()`** - Catch typos and unexpected fields935. **Keep refinements simple** - Complex business logic should be separate946. **Reuse schemas** - Define once, reference everywhere957. **Document complex schemas** - Use TypeScript JSDoc comments9697## Workflow9899When working with Zod:1001011. **Define schemas** - Start with the shape of your data1022. **Add validations** - Layer on constraints (min, max, regex, etc.)1033. **Add custom errors** - Make validation messages user-friendly1044. **Infer types** - Use `z.infer<typeof schema>` for TypeScript types1055. **Parse safely** - Use `.safeParse()` and handle errors gracefully1066. **Test edge cases** - Validate your validation logic107108## Resources109110- **Migration Guide**: [references/migration-from-v3.md](references/migration-from-v3.md)111- **Core API Reference**: [references/core-api.md](references/core-api.md)112- **Common Patterns**: [references/common-patterns.md](references/common-patterns.md)113- **Official Docs**: https://zod.dev/v4114115---116117## Custom Instructions118119<!--120USER: Add your own custom directions and information below this line.121You can include:122- Project-specific validation patterns123- Team conventions for error messages124- Custom schema factories or utilities125- Integration patterns with your stack (React Hook Form, tRPC, etc.)126- Common gotchas specific to your codebase127-->128129---130> Source: [boneskull/claude-plugins](https://github.com/boneskull/claude-plugins) — distributed by [TomeVault](https://tomevault.io).131<!-- tomevault:4.0:skill_md:2026-06-19 -->