Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsCommunityBlog
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Zod Validation Patterns

ASecurity

This skill provides comprehensive patterns for using Zod validation library in TypeScript applications. It ensures input validation is done correctly, securely, and consistently across the codebase.

416 stars
0 votes
2 copies
13 views
Added 2/8/2026
developmenttypescriptrustnextjstestingapidatabaseperformancedocumentation

Works with

cliapi

Security Analysis

A100/100

Scanned 2/12/2026

Install to Claude Code

$npx -y skills add aiskillstore/marketplace --skill zod-validation-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Zod Validation Patterns?

Add the live security badge to your README — it updates automatically with every re-scan.

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

More formats (shields.io, HTML) on the badges page.

Download with Pro
Files
SKILL.md
---
name: zod-validation-patterns
description: This skill provides comprehensive patterns for using Zod validation library in TypeScript applications. It ensures input validation is done correctly, securely, and consistently across the codebase.
---

# Zod Validation Patterns Skill

**Use this skill when:** Working with user input validation, API request validation, form data validation, or data transformation in Quetrex.

## Purpose

This skill provides comprehensive patterns for using Zod validation library in TypeScript applications. It ensures input validation is done correctly, securely, and consistently across the codebase.

## What's Covered

1. **[Schema Patterns](./schema-patterns.md)** - Complete guide to all Zod schema types
   - Primitives (string, number, boolean, date)
   - Collections (array, object, map, set, record)
   - Advanced types (union, intersection, discriminated unions)
   - Optional/nullable patterns
   - Branded types and recursive schemas

2. **[Error Handling](./error-handling.md)** - Robust error management
   - Custom error messages
   - Internationalization (i18n)
   - Error formatting for UI display
   - Safe parsing patterns
   - Error recovery strategies

3. **[Refinements](./refinements.md)** - Custom validation logic
   - Basic and chained refinements
   - Cross-field validation
   - Conditional validation
   - Business logic validation
   - File upload validation

4. **[Transforms](./transforms.md)** - Data transformation and normalization
   - Type coercion
   - Data cleaning and normalization
   - Computed fields
   - Preprocessing patterns

5. **[Async Validation](./async-validation.md)** - Asynchronous validation patterns
   - Database uniqueness checks
   - API validations
   - Concurrent async validations
   - Error handling and timeouts

6. **[Type Inference](./type-inference.md)** - TypeScript type extraction
   - z.infer patterns
   - Input vs output types
   - Generic schema types
   - Discriminated union inference

7. **[API Integration](./api-integration.md)** - Next.js integration patterns
   - API routes validation
   - Server Actions validation
   - Form data and file uploads
   - Error response formatting

8. **[Common Schemas](./common-schemas.md)** - Reusable schema library
   - Email, password, phone validation
   - URL, UUID, date schemas
   - Address, credit card validation
   - Username, slug, color schemas

## Quick Start

### Basic Usage

```typescript
import { z } from 'zod'

// Define schema
const userSchema = z.object({
  email: z.string().email(),
  age: z.number().int().positive(),
  role: z.enum(['admin', 'user'])
})

// Parse data (throws on error)
const user = userSchema.parse(data)

// Safe parse (returns result object)
const result = userSchema.safeParse(data)
if (result.success) {
  console.log(result.data)
} else {
  console.error(result.error)
}
```

### Type Inference

```typescript
// Extract TypeScript type from schema
type User = z.infer<typeof userSchema>
// { email: string; age: number; role: 'admin' | 'user' }
```

### API Route Example

```typescript
// src/app/api/users/route.ts
import { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'

const createUserSchema = z.object({
  email: z.string().email(),
  password: z.string().min(8)
})

export async function POST(request: NextRequest) {
  const body = await request.json()

  const result = createUserSchema.safeParse(body)
  if (!result.success) {
    return NextResponse.json(
      { error: 'Validation failed', details: result.error.format() },
      { status: 400 }
    )
  }

  // Process validated data
  const { email, password } = result.data
  // ...
}
```

## When to Use This Skill

### DO Use for:
- **API request validation** - All incoming data to API routes
- **Form submission validation** - Client and server-side
- **Database input validation** - Before inserting/updating
- **Configuration validation** - Environment variables, config files
- **File upload validation** - Size, type, content validation
- **External API responses** - Validate third-party data

### DON'T Use for:
- **Simple type checks** - Use TypeScript types when validation isn't needed
- **Runtime performance-critical paths** - Validation has overhead
- **Already validated data** - Don't re-validate trusted internal data

## Best Practices

1. **Validate at boundaries** - API routes, Server Actions, external data sources
2. **Use safe parsing** - Prefer `safeParse()` over `parse()` for better error handling
3. **Provide clear error messages** - Customize messages for user-facing validation
4. **Reuse common schemas** - Use schemas from `common-schemas.md`
5. **Type inference** - Always use `z.infer<typeof schema>` for TypeScript types
6. **Test edge cases** - Write tests for validation logic
7. **Document complex schemas** - Add JSDoc comments for business rules

## Common Patterns

### 1. Optional Fields with Defaults

```typescript
const configSchema = z.object({
  timeout: z.number().int().positive().default(30),
  retries: z.number().int().min(0).default(3),
  debug: z.boolean().optional()
})
```

### 2. Conditional Required Fields

```typescript
const addressSchema = z.object({
  country: z.string(),
  state: z.string().optional()
}).refine(
  data => data.country === 'US' ? !!data.state : true,
  { message: 'State is required for US addresses', path: ['state'] }
)
```

### 3. Transform and Validate

```typescript
const emailSchema = z.string()
  .trim()
  .toLowerCase()
  .email()
```

### 4. Discriminated Unions

```typescript
const eventSchema = z.discriminatedUnion('type', [
  z.object({ type: z.literal('click'), x: z.number(), y: z.number() }),
  z.object({ type: z.literal('keypress'), key: z.string() })
])
```

### 5. Async Database Check

```typescript
const usernameSchema = z.string()
  .min(3)
  .max(20)
  .regex(/^[a-zA-Z0-9_-]+$/)
  .refine(async (username) => {
    const existing = await db.user.findUnique({ where: { username } })
    return !existing
  }, { message: 'Username already taken' })
```

## Integration with Quetrex

### TypeScript Strict Mode Compliance

All schemas must work with TypeScript strict mode:
- No `any` types
- No `@ts-ignore` comments
- Explicit type inference with `z.infer`

### Testing Requirements

Validation logic requires comprehensive tests:
- **Happy path** - Valid data passes
- **Edge cases** - Boundary values, empty strings, null/undefined
- **Error cases** - Invalid data produces expected errors
- **Custom validations** - All refinements and transforms tested

### Server Actions Pattern

```typescript
'use server'

import { z } from 'zod'

const createProjectSchema = z.object({
  name: z.string().min(1).max(100),
  description: z.string().optional()
})

export async function createProject(formData: FormData) {
  const result = createProjectSchema.safeParse({
    name: formData.get('name'),
    description: formData.get('description')
  })

  if (!result.success) {
    return { error: result.error.format() }
  }

  // Process validated data
  return { success: true, data: result.data }
}
```

## Resources

- **Zod Documentation**: https://zod.dev/
- **TypeScript Handbook**: https://www.typescriptlang.org/docs/handbook/
- **Next.js Server Actions**: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations

## Navigation

Start with:
1. **[Schema Patterns](./schema-patterns.md)** - Learn all schema types
2. **[Common Schemas](./common-schemas.md)** - Use ready-made schemas
3. **[API Integration](./api-integration.md)** - Integrate with Next.js

Then explore:
- **[Error Handling](./error-handling.md)** - Better error messages
- **[Refinements](./refinements.md)** - Custom validation logic
- **[Transforms](./transforms.md)** - Data transformation
- **[Async Validation](./async-validation.md)** - Database/API checks
- **[Type Inference](./type-inference.md)** - Advanced TypeScript patterns

---

*Last updated: 2025-11-23 | Zod v4.1.12*

Attribution

aiskillstoreaiskillstore
View sourceMore from aiskillstore →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

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 (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Browser Extension Developer

Use this skill when developing or maintaining browser extension code in the `browser/` directory, including Chrome/Firefox/Edge compatibility, content scripts, background scripts, or i18n updates.

284722 votes

Seo Optimizer

SEO optimization with keyword analysis, readability assessment, technical validation, content quality. Use for search rankings, blog posts, content audits, or encountering keyword density, readability scores, meta tags, schema markup errors.

2192 votes

Google Official Seo Guide

Official Google SEO guide covering search optimization, best practices, Search Console, crawling, indexing, and improving website search visibility based on official Google documentation

1862 votes

Tanstack Start

Build a full-stack TanStack Start app on Cloudflare Workers from scratch — SSR, file-based routing, server functions, D1+Drizzle, better-auth, Tailwind v4+shadcn/ui. Use whenever the user mentions TanStack Start, asks to scaffold a full-stack Cloudflare app with SSR, wants an SSR dashboard, or asks for a React 19 + Cloudflare Workers app with file-based routing and server functions — even if they don't name TanStack Start specifically. No template repo — Claude generates every file fresh per ...

9881 votes

Pentest

PTES-aligned adversarial security audit for backend, frontend, and mobile applications. Produces a CVSS-scored Hacker Report with verified PoCs and phased remediation.

5491 votes
View all in development →