zod-v4
Zod v4 Handbook
Purpose
Use this as the single source of truth for Zod 4 usage in this repository. Keep lookup cheap: route to one file first, then follow See also only if that file is not enough.
Routing Rules
- Do not read this handbook linearly.
- Pick exactly one target file first.
- Start with a
sections/*.mdfile for broad tasks. - Start with a
topics/*.mdfile for narrow API questions. - Follow
See alsoonly when the first file is insufficient. - Ignore release notes, versioning, migration, and library-author internals unless the user explicitly asks for them.
Quick Picks (Task -> File)
- Define a first schema, parse data, infer types ->
sections/00-basics.md - Choose a primitive or literal schema ->
sections/10-primitives.md - Validate string formats like email, UUID, URL, ISO datetime ->
topics/string-formats.md - Build or reshape object schemas ->
topics/object-shape-control.md - Validate arrays, tuples, records, maps, or sets ->
topics/collection-types.md - Model variants or optional/null values ->
topics/unions-and-optionality.md - Add custom validation rules ->
topics/refinements.md - Model recursive trees or graphs ->
topics/recursive-schemas.md - Transform input, preprocess, or pipe schemas ->
sections/40-transforms-codecs.md - Use defaults, prefaults, or catch fallbacks ->
topics/fallbacks.md - Decode and encode between types ->
topics/codecs.md - Customize error messages ->
topics/error-customization.md - Convert errors into UI-friendly structures ->
topics/error-formatting.md - Emit or ingest JSON Schema ->
topics/json-schema.md - Attach metadata or use registries ->
topics/metadata-registries.md - Validate special runtime values like
File, class instances, or functions ->topics/special-types.md
By Domain
- Foundations (schemas, parsing, inference, async boundaries) ->
sections/00-basics.md - Primitive schemas (strings, numbers, booleans, enums, literals, dates) ->
sections/10-primitives.md - Object and collection modeling ->
sections/20-objects-collections.md - Composition and validation logic ->
sections/30-composition.md - Transforms, preprocess, defaults, codecs ->
sections/40-transforms-codecs.md - Errors and reporting ->
sections/50-errors.md - Metadata and JSON Schema ->
sections/60-json-schema-metadata.md
By Feature
parse/safeParse/ async parsing ->sections/00-basics.mdz.input/z.output/z.infer->sections/00-basics.md- String formats ->
topics/string-formats.md z.object/z.strictObject/z.looseObject/catchall->topics/object-shape-control.md- Arrays / tuples / records / maps / sets ->
topics/collection-types.md optional/nullable/nullish/ unions ->topics/unions-and-optionality.md- Recursive schemas ->
topics/recursive-schemas.md refine/superRefine/check->topics/refinements.mdpreprocess/transform/pipe->sections/40-transforms-codecs.mddefault/prefault/catch->topics/fallbacks.mdz.codec/z.decode/z.encode->topics/codecs.md- Error maps and per-parse customization ->
topics/error-customization.md z.treeifyError/z.flattenError/z.prettifyError->topics/error-formatting.mdz.toJSONSchema/z.toJSONSchema(schema, { io })->topics/json-schema.mdmeta/describe/ registries /z.globalRegistry->topics/metadata-registries.mdinstanceof/file/function/ branded / readonly ->topics/special-types.md
Section Index
sections/00-basics.md- define schemas, parse values, infer input/output types, async parsingsections/10-primitives.md- primitives, literals, enums, dates, unknown/any/neversections/20-objects-collections.md- objects, unknown-key policy, arrays, tuples, records, maps, setssections/30-composition.md- unions, discriminated unions, intersections, recursion, refinementssections/40-transforms-codecs.md- preprocess, transforms, pipes, defaults, prefaults, catch, codecssections/50-errors.md- error handling, customization, formatting for apps and UIssections/60-json-schema-metadata.md- metadata registries and JSON Schema conversion
Topic Index
topics/string-formats.md- email, URL, UUID, ISO datetime, IPs, hashes,stringbooltopics/object-shape-control.md- object derivation and unknown-key handlingtopics/collection-types.md- arrays, tuples, records, maps, setstopics/unions-and-optionality.md- optional, nullable, nullish, unions, discriminated unions, intersectionstopics/recursive-schemas.md- self-referential schemas and getter-based recursiontopics/refinements.md- custom validation logic and issue reportingtopics/fallbacks.md- defaults, prefaults, and catch fallbackstopics/codecs.md- bidirectional decode/encode flowstopics/error-customization.md- schema-level and per-parse error customizationtopics/error-formatting.md- tree, flat, and pretty error outputtopics/json-schema.md- JSON Schema export and representability limitstopics/metadata-registries.md- metadata storage, lookup, and registriestopics/special-types.md- files, functions, class instances, branded, readonly, custom
Conventions
- Section files: What it is | When to use | Quick rules | Minimal examples | Common pitfalls | See also
- Topic files: same structure, compact, task-oriented
- Paths are relative to this
SKILL.md
When to Use This Handbook
- Implementing API request or response validation
- Defining schema-driven config, form, or env validation
- Reusing one schema for runtime parsing and static typing
- Debugging
ZodErrorhandling and user-facing messages - Converting Zod schemas to JSON Schema or attaching metadata
Package References
- Use
zodby default. - Only reach for
zod/miniorzod/v4/coreif the user explicitly asks about those packages.
More from tstelzer/skills
skill-creator
Guide for creating or updating simple, standalone skills with a single SKILL.md file
53discovery
Use for deep design discovery and problem understanding before planning or implementation. Trigger when the user wants to think deeply, build knowledge, surface edge-cases, ask many questions, or explore the problem space without producing plans or code; save a design artifact only when durable context is requested or clearly useful.
44effect-vitest
Testing Effect programs with vitest. Use when writing tests for effect-based code.
32effect
Explains basic effect usage and terms. Use when using effect in typescript.
31review-plan-implementation
Review code changes against a plan using the plan location and implementing git commit(s).
31effect-http-server
Explains how to define http servers with effect. Use only when a top-level server is, or needs to be written in effect.
30