install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/zod" ~/.claude/skills/comeonoliver-skillshub-zod && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/zod/SKILL.mdsource content
Zod Best Practices
Comprehensive schema validation guide for Zod in TypeScript applications. Contains 43 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Writing new Zod schemas
- Choosing between parse() and safeParse()
- Implementing type inference with z.infer
- Handling validation errors for user feedback
- Composing complex object schemas
- Using refinements and transforms
- Optimizing bundle size and validation performance
- Reviewing Zod code for best practices
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Schema Definition | CRITICAL | |
| 2 | Parsing & Validation | CRITICAL | |
| 3 | Type Inference | HIGH | |
| 4 | Error Handling | HIGH | |
| 5 | Object Schemas | MEDIUM-HIGH | |
| 6 | Schema Composition | MEDIUM | |
| 7 | Refinements & Transforms | MEDIUM | |
| 8 | Performance & Bundle | LOW-MEDIUM | |
Quick Reference
1. Schema Definition (CRITICAL)
- Use correct primitive schemas for each typeschema-use-primitives-correctly
- Use z.unknown() instead of z.any() for type safetyschema-use-unknown-not-any
- Avoid overusing optional fieldsschema-avoid-optional-abuse
- Apply string validations at schema definitionschema-string-validations
- Use enums for fixed string valuesschema-use-enums
- Use coercion for form and query dataschema-coercion-for-form-data
2. Parsing & Validation (CRITICAL)
- Use safeParse() for user inputparse-use-safeparse
- Use parseAsync for async refinementsparse-async-for-async-refinements
- Handle all validation issues not just firstparse-handle-all-issues
- Validate at system boundariesparse-validate-early
- Avoid validating same data twiceparse-avoid-double-validation
- Never trust JSON.parse outputparse-never-trust-json
3. Type Inference (HIGH)
- Use z.infer instead of manual typestype-use-z-infer
- Distinguish z.input from z.infer for transformstype-input-vs-output
- Export both schemas and inferred typestype-export-schemas-and-types
- Use branded types for domain safetytype-branded-types
- Enable TypeScript strict modetype-enable-strict-mode
4. Error Handling (HIGH)
- Provide custom error messageserror-custom-messages
- Use flatten() for form error displayerror-use-flatten
- Use issue.path for nested error locationerror-path-for-nested
- Implement internationalized error messageserror-i18n
- Return false instead of throwing in refineerror-avoid-throwing-in-refine
5. Object Schemas (MEDIUM-HIGH)
- Choose strict() vs strip() for unknown keysobject-strict-vs-strip
- Use partial() for update schemasobject-partial-for-updates
- Use pick() and omit() for schema variantsobject-pick-omit
- Use extend() for adding fieldsobject-extend-for-composition
- Distinguish optional() from nullable()object-optional-vs-nullable
- Use discriminated unions for type narrowingobject-discriminated-unions
6. Schema Composition (MEDIUM)
- Extract shared schemas into reusable modulescompose-shared-schemas
- Use intersection() for type combinationscompose-intersection
- Use z.lazy() for recursive schemascompose-lazy-recursive
- Use preprocess() for data normalizationcompose-preprocess
- Use pipe() for multi-stage validationcompose-pipe
7. Refinements & Transforms (MEDIUM)
- Choose refine() vs superRefine() correctlyrefine-vs-superrefine
- Distinguish transform() from refine() and coerce()refine-transform-coerce
- Add path to refinement errorsrefine-add-path
- Use default() for optional fields with defaultsrefine-defaults
- Use catch() for fault-tolerant parsingrefine-catch
8. Performance & Bundle (LOW-MEDIUM)
- Cache schema instancesperf-cache-schemas
- Use Zod Mini for bundle-sensitive applicationsperf-zod-mini
- Avoid dynamic schema creation in hot pathsperf-avoid-dynamic-creation
- Lazy load large schemasperf-lazy-loading
- Optimize large array validationperf-arrays
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- Individual rules:
references/{prefix}-{slug}.md
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md
Related Skills
- For React Hook Form integration, see
skillreact-hook-form - For API client generation, see
skillorval