zod
Original:🇺🇸 English
Not Translated
Zod schema validation best practices for type safety, parsing, and error handling. This skill should be used when defining z.object schemas, using z.string validations, safeParse, or z.infer. This skill does NOT cover React Hook Form integration patterns (use react-hook-form skill) or OpenAPI client generation (use orval skill).
3installs
Added on
NPX Install
npx skill4agent add thongdn-it/react-agent-skills zodSKILL.md 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 type
schema-use-primitives-correctly - - Use z.unknown() instead of z.any() for type safety
schema-use-unknown-not-any - - Avoid overusing optional fields
schema-avoid-optional-abuse - - Apply string validations at schema definition
schema-string-validations - - Use enums for fixed string values
schema-use-enums - - Use coercion for form and query data
schema-coercion-for-form-data
2. Parsing & Validation (CRITICAL)
- - Use safeParse() for user input
parse-use-safeparse - - Use parseAsync for async refinements
parse-async-for-async-refinements - - Handle all validation issues not just first
parse-handle-all-issues - - Validate at system boundaries
parse-validate-early - - Avoid validating same data twice
parse-avoid-double-validation - - Never trust JSON.parse output
parse-never-trust-json
3. Type Inference (HIGH)
- - Use z.infer instead of manual types
type-use-z-infer - - Distinguish z.input from z.infer for transforms
type-input-vs-output - - Export both schemas and inferred types
type-export-schemas-and-types - - Use branded types for domain safety
type-branded-types - - Enable TypeScript strict mode
type-enable-strict-mode
4. Error Handling (HIGH)
- - Provide custom error messages
error-custom-messages - - Use flatten() for form error display
error-use-flatten - - Use issue.path for nested error location
error-path-for-nested - - Implement internationalized error messages
error-i18n - - Return false instead of throwing in refine
error-avoid-throwing-in-refine
5. Object Schemas (MEDIUM-HIGH)
- - Choose strict() vs strip() for unknown keys
object-strict-vs-strip - - Use partial() for update schemas
object-partial-for-updates - - Use pick() and omit() for schema variants
object-pick-omit - - Use extend() for adding fields
object-extend-for-composition - - Distinguish optional() from nullable()
object-optional-vs-nullable - - Use discriminated unions for type narrowing
object-discriminated-unions
6. Schema Composition (MEDIUM)
- - Extract shared schemas into reusable modules
compose-shared-schemas - - Use intersection() for type combinations
compose-intersection - - Use z.lazy() for recursive schemas
compose-lazy-recursive - - Use preprocess() for data normalization
compose-preprocess - - Use pipe() for multi-stage validation
compose-pipe
7. Refinements & Transforms (MEDIUM)
- - Choose refine() vs superRefine() correctly
refine-vs-superrefine - - Distinguish transform() from refine() and coerce()
refine-transform-coerce - - Add path to refinement errors
refine-add-path - - Use default() for optional fields with defaults
refine-defaults - - Use catch() for fault-tolerant parsing
refine-catch
8. Performance & Bundle (LOW-MEDIUM)
- - Cache schema instances
perf-cache-schemas - - Use Zod Mini for bundle-sensitive applications
perf-zod-mini - - Avoid dynamic schema creation in hot paths
perf-avoid-dynamic-creation - - Lazy load large schemas
perf-lazy-loading - - Optimize large array validation
perf-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.mdRelated Skills
- For React Hook Form integration, see skill
react-hook-form - For API client generation, see skill
orval