zod-best-practices
Zod Best Practices
Use this skill for core Zod authoring, review, migration, and explanation. Prefer modern APIs from the official docs, call out legacy or deprecated patterns explicitly, and keep recommendations practical enough to apply directly in real TypeScript codebases.
Use This Skill When
- Writing new Zod schemas for TypeScript applications
- Reviewing an existing Zod schema for correctness, maintainability, or migration issues
- Modernizing older Zod code to current patterns
- Choosing parsing, error customization, or error formatting APIs
- Reasoning about type inference, advanced schemas, coercion, transforms, checks, or Zod Mini tradeoffs
Out Of Scope
- Framework-specific integration patterns where the main problem is the library wiring, not the Zod schema itself
- OpenAPI generation or client-generation workflows
- ORM, router, or UI library specifics unless the question is still primarily about the Zod schema design
Working Style
- Identify whether the task is review, modernization, schema authoring, or API explanation.
- Prefer review and modernization first when the user supplied code.
- Read only the relevant reference file(s) below instead of loading everything.
- Prefer current APIs in examples and name deprecated or legacy alternatives when they appear in existing code.
- Keep recommendations practical: prefer the simplest schema that preserves type safety, clear errors, and maintainable composition.
Reference Map
references/schema-fundamentals.md- Use for primitives, object modeling, arrays, records, enums, nullability, defaults, and string-format helpers.
references/validation-flow-and-errors.md- Use for
parsevssafeParse, async parsing, custom errors, issue handling, and error formatting.
- Use for
references/type-inference-and-advanced-patterns.md- Use for
z.infer,z.input,z.output, discriminated unions, branded types, recursion, and readonly schemas.
- Use for
references/composition-transforms-and-coercion.md- Use for
.extend(), shape spreading,.pick(),.omit(),.partial(),.pipe(),.transform(),z.coerce,z.preprocess,.refine(),.check(), and legacy.superRefine().
- Use for
references/migration-and-deprecations.md- Use for old-to-new replacements, removed APIs, and behavior changes that commonly trip up existing code.
references/performance-and-zod-mini.md- Use for schema reuse, TypeScript performance, tree-shaking, and when Zod Mini is or is not worth it.
Response Requirements
- Prefer modern syntax in all new code.
- When reviewing code, separate concrete issues from optional improvements.
- When replacing deprecated APIs, show before/after snippets if the user gave code.
- Do not recommend legacy APIs as the default when a current alternative exists.
- If a legacy API still works for backward compatibility, say that clearly and still prefer the modern form.
- If an integration-specific question is mostly about another framework or library, keep the answer centered on the Zod portion and avoid drifting into tool-specific tutorials.
Core Defaults
- Prefer top-level format helpers like
z.email()andz.uuid()over deprecated method forms such asz.string().email(). - Prefer
safeParsefor expected user-input validation andparsewhen throwing is intentional. - Prefer the unified
errorAPI over oldermessage,required_error,invalid_type_error, orerrorMappatterns. - Prefer
z.strictObject()orz.looseObject()over legacy.strict()or.passthrough()when the object mode should be explicit. - Prefer
.extend()or object shape spreading over deprecated.merge(). - Prefer
z.enum()over deprecatedz.nativeEnum(). - Prefer top-level error formatting helpers such as
z.treeifyError(),z.flattenError(), andz.prettifyError()over deprecated instance methods. - Prefer
.refine()for simple validation,.check()for lower-level composition or multi-issue control, and treat.superRefine()as legacy-but-common in existing code.
Provenance
This skill was authored against the official Zod documentation available at the time of creation, specifically version 4.3.6 materials.
More from mkgp-dev/codex-skills
tanstack-query-best-practices
Best-practice guidance for designing, reviewing, debugging, and modernizing TanStack Query v5 usage across React, Preact, Solid, Vue, Svelte, and Angular. Use this skill whenever the user is working with TanStack Query or React Query, `QueryClient` setup, `useQuery`, `useMutation`, `useInfiniteQuery`, query keys, invalidation, optimistic updates, prefetching, hydration, cache tuning, stale UI, or server-state boundaries and needs current guidance instead of stale v4-era patterns.
8zustand-state-management
Best-practice guidance for designing, reviewing, and modernizing Zustand state in modern TypeScript React apps. Use this skill whenever the user is writing or reviewing `create`, `createStore`, `useStore`, `useShallow`, `persist`, `devtools`, `subscribeWithSelector`, `createWithEqualityFn`, slices, Next.js store setup, hydration behavior, or selector-related rerender problems in Zustand v5 code.
7tanstack-router-best-practices
Best-practice guidance for designing, reviewing, debugging, and modernizing TanStack Router routing in React apps, especially file-based route trees, `Link` and `useNavigate`, `validateSearch`, `beforeLoad`, loaders, `notFoundComponent`, auth guards, code splitting, and TanStack Query integration. Use this whenever the user is working with `@tanstack/react-router`, TanStack Start routing, route organization, URL state, or navigation behavior and needs current v1 guidance instead of stale class-based or React Router patterns.
7changelog-maintenance
Best-practice guidance for creating, reviewing, and maintaining high-signal `CHANGELOG.md` entries for versioned software projects. Use this skill whenever the user wants to add or rewrite changelog entries, organize release history, turn completed work into structured version notes, decide what belongs in `Added`/`Changed`/`Fixed`/`Deprecated`/`Removed`/`Security`, or improve noisy release notes into maintainable repository changelogs.
5shadcn-ui-best-practices
Best-practice guidance for authoring, reviewing, debugging, and modernizing shadcn/ui usage in React applications. Use this skill whenever the user is working with shadcn/ui components, `components.json`, Dialog or Sheet flows, Field or FieldGroup forms, semantic tokens, theming, Tailwind v4 integration, or wants to replace ad hoc Tailwind-heavy UI with maintainable component-first patterns.
2react-hook-form-with-zod
Best-practice guidance for building forms with React Hook Form in modern TypeScript React apps, with Zod integration when schema validation is useful. Use this skill whenever the user is writing or reviewing `useForm`, `register`, `useController`, `Controller`, `useFieldArray`, `useWatch`, `useFormState`, `FormProvider`, `useFormContext`, `defaultValues`, `setValue`, `reset`, `trigger`, `setError`, or accessible React Hook Form validation flows, including cases where Zod and `zodResolver` should be added or modernized.
1