Create Comprehensive Zod Schema Validation for Forms and APIs
Build a complete Zod validation system with derived schemas, cross-field rules, transforms, and framework integration.
๐ The Prompt
Build a complete Zod validation schema system for a [APPLICATION_TYPE] application that validates [DATA_ENTITY] data across both client-side forms and server-side API endpoints.
**Schema Requirements:**
1. **Base Entity Schema**: Define a `[ENTITY_NAME]Schema` with these fields:
- [FIELD_1]: [ZOD_TYPE] โ [CONSTRAINTS, e.g., min length 2, max 50, regex pattern]
- [FIELD_2]: [ZOD_TYPE] โ [CONSTRAINTS, e.g., valid email, ends with specific domain]
- [FIELD_3]: [ZOD_TYPE] โ [CONSTRAINTS, e.g., enum of allowed values]
- [FIELD_4]: [ZOD_TYPE] โ [CONSTRAINTS, e.g., positive integer, max 999]
- [FIELD_5]: [ZOD_TYPE] โ optional, [CONSTRAINTS]
Include custom error messages for every validation rule in [LANGUAGE].
2. **Derived Schemas**: Generate these from the base schema using Zod methods:
- `Create[ENTITY]Schema`: Omits auto-generated fields (id, createdAt)
- `Update[ENTITY]Schema`: Makes all fields partial, requires at least one field
- `[ENTITY]QuerySchema`: For GET list endpoints with pagination (`page`, `limit`), sorting (`sortBy`, `order`), and optional filter fields
- `[ENTITY]PatchSchema`: Allows only specific fields [PATCHABLE_FIELDS] to be updated
3. **Advanced Validations**:
- Cross-field validation using `.refine()` or `.superRefine()`: [CROSS_FIELD_RULE, e.g., endDate must be after startDate]
- Conditional validation: If [FIELD_A] equals [VALUE], then [FIELD_B] becomes required
- Custom transform: Normalize [FIELD] (e.g., trim whitespace, lowercase email) using `.transform()`
- Async validation with `.refine()`: Check [UNIQUENESS_FIELD] against [DATA_SOURCE]
4. **Integration Code**:
- Export inferred TypeScript types using `z.infer<typeof schema>`
- Write a reusable `validateRequest(schema, data)` wrapper that returns `{ success, data, errors }` with formatted field-level errors
- Show integration with [FRAMEWORK] (e.g., Next.js API route, Express middleware, React Hook Form)
5. **Test Cases**: Write 5 test cases covering: valid data, missing required fields, type coercion, cross-field failure, and edge cases for [EDGE_CASE].
Provide all code in TypeScript with clear file organization and imports.
๐ก Tips for Better Results
Specify your form library (React Hook Form, Formik, VeeValidate) for direct resolver integration code. Include your actual field names and business rules to get production-ready schemas instead of generic examples. Mention if you need i18n error messages so the prompt generates a message map pattern.
๐ฏ Use Cases
Full-stack TypeScript developers use this when implementing consistent validation logic that is shared between frontend forms and backend API handlers to ensure data integrity.
๐ Related Prompts
๐ป Coding
beginner
Explain Code Like Im a Beginner
Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.
๐ป Coding
beginner
Debug My Code and Explain the Fix
Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.
๐ป Coding
intermediate
Write Unit Tests for My Code
Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.
๐ป Coding
intermediate
Convert Code Between Languages
Convert code between any programming languages while maintaining idiomatic patterns and best practices.
๐ป Coding
intermediate
Write a REST API Endpoint
Generate production-ready REST API endpoints with validation, error handling, and documentation.
๐ป Coding
advanced
Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment
Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.