Skip to content
Back to skills

Zod Schema Validation

ASecurity

Best practices for Zod schema validation and type inference in TypeScript applications.

  • 248 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added February 10, 2026
developmenttypescriptgoreactapiperformance

Works with

  • api

Security analysis

A100/100

Scanned February 12, 2026

npx -y skills add Mindrally/skills --skill zod-schema-validation --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Zod Schema Validation?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Zod Schema Validation
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mindrally-zod-schema-validation/badge)](https://www.skillsdirectory.com/skills/mindrally-zod-schema-validation)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: zod-schema-validation
description: Best practices for Zod schema validation and type inference in TypeScript applications.
---

# Zod Schema Validation

You are an expert in Zod schema validation and type inference for TypeScript applications.

## Core Principles

- Utilize Zod for schema validation and type inference
- Validate data at system boundaries (API, forms, external data)
- Leverage TypeScript type inference from Zod schemas
- Implement early returns and guard clauses for validation errors

## Schema Design

### Basic Schema
```typescript
import { z } from 'zod'

const UserSchema = z.object({
  id: z.string().uuid(),
  email: z.string().email(),
  name: z.string().min(1).max(100),
  age: z.number().int().positive().optional(),
  role: z.enum(['admin', 'user', 'guest']),
  createdAt: z.date(),
})

type User = z.infer<typeof UserSchema>
```

### Best Practices
- Define schemas close to where they're used
- Use `.infer` to derive TypeScript types
- Compose schemas using `.extend()`, `.merge()`, `.pick()`, `.omit()`
- Create reusable base schemas for common patterns

## Validation Patterns

### Safe Parsing
```typescript
const result = UserSchema.safeParse(data)
if (!result.success) {
  console.error(result.error.format())
  return
}
// result.data is typed as User
```

### Transform and Refine
```typescript
const schema = z.string()
  .transform((val) => val.trim().toLowerCase())
  .refine((val) => val.length > 0, 'Cannot be empty')
```

## Form Integration

- Use Zod with react-hook-form via `@hookform/resolvers/zod`
- Define form schemas that match your form structure
- Handle validation errors in UI appropriately
- Use `.partial()` for optional update forms

## API Validation

- Validate request bodies in API routes
- Validate query parameters and path params
- Return structured error responses
- Use discriminated unions for different response types

## Error Handling

- Implement custom error messages for better UX
- Use `.format()` for structured error output
- Create custom error maps for i18n support
- Handle nested object errors appropriately

## Advanced Patterns

### Discriminated Unions
```typescript
const ResultSchema = z.discriminatedUnion('status', [
  z.object({ status: z.literal('success'), data: UserSchema }),
  z.object({ status: z.literal('error'), message: z.string() }),
])
```

### Recursive Schemas
```typescript
const CategorySchema: z.ZodType<Category> = z.lazy(() =>
  z.object({
    name: z.string(),
    children: z.array(CategorySchema),
  })
)
```

## Performance

- Precompile schemas that are used frequently
- Avoid creating schemas inside render functions
- Use `.passthrough()` or `.strict()` intentionally
- Consider partial validation for large objects

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…