zod

A set of coding rules for Zod, a TypeScript library that checks whether data matches defined schemas. It covers validation, type inference, data conversion, transformations, and custom checks.

In plain words
What is it for?
Use it for API requests, form data, query parameters, configuration, schemas, inferred types, data normalization, and custom validation rules.
Why use it?
It keeps runtime validation and TypeScript types aligned, while making invalid input and validation errors easier to handle consistently.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/zod
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection
Per session 651 This file is loaded in full into every session.
When invoked 651 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00651 $0.00651
Opus 5 $0.00326 $0.00326
Sonnet 5 $0.00130 $0.00130
Haiku 4.5 $0.00065 $0.00065

Measured 2d ago against content hash f1348c5dad7d, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

zod scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules-mdc/frameworks/zod.mdc · 55 lines

How it starts

The opening of the file, as written. The whole thing — 55 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Zod Cursor Rules

You are an expert in Zod schema validation. Follow these rules:

Schema Design

  • Define schemas as the single source of truth — infer TypeScript types with z.infer
  • Never duplicate types manually when a Zod schema exists
  • Colocate schemas with their usage (API route, form, etc.)
  • Name schemas with Schema suffix: UserSchema, CreatePostSchema
  • Export both the schema and inferred type together

Primitives & Modifiers

  • Use z.string().min(1) instead of z.string() for required strings — empty string passes z.string()
  • Use z.coerce.number() for form inputs and query params, not z.number()
  • Prefer z.literal() for exact values over z.enum() with one member
  • Use z.discriminatedUnion() over z.union() when objects share a type/kind field — better errors and performance

Transforms & Pipes

  • Use .transform() to normalize data at parse time (trim strings, lowercase emails)
  • Chain .pipe() for multi-step validation: parse string → coerce to number → validate range
  • Never mutate input inside .transform() — always return new values
  • Keep transforms pure — no side effects, no API calls inside .transform()

Refinements

  • Use .refine() for single-field custom validation with a clear error message
  • Use .superRefine() for cross-field validation (password confirmation, date ranges)
  • Always provide a message parameter — default Zod errors are unhelpful to users
  • Set the path in superRefine errors to attach them to specific fields

Error Handling

  • Use schema.safeParse() in API routes — never let .parse() throw unhandled
  • Format errors with z.ZodError.flatten() for form-friendly { fieldErrors, formErrors }
  • Map Zod errors to user-facing messages — don't expose raw Zod error strings
  • Create a shared parseOrThrow utility that wraps safeParse with consistent error responses

Anti-Patterns — Do NOT

  • ❌ z.any() or z.unknown() without immediately piping into a real schema
  • ❌ Wrapping Zod schemas in try/catch when safeParse exists
  • ❌ Using .optional() when you mean .nullable() — they're different
  • ❌ Defining schemas inside components or functions — define at module level
  • ❌ Skipping .strip() or .strict() — pick one. Default (strip) is usually correct for APIs
  • ❌ Using z.object().merge() when .extend() works — merge creates a new ZodObject, extend inherits

Read the full file on GitHub · 55 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 55 lines · 651 tokens per session scan A f1348c5dad7d

Subscribe to this mod's changes

zod is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 651 tokens to every session, about $0.0033 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.