|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +Rule: Shared Zod Schemas for Client & Server Validation in Next.js |
| 7 | +When implementing features that require data validation using Zod schemas on both the client-side (e.g., in a React form within a Client Component) and the server-side (e.g., within a Next.js Server Action): |
| 8 | +Centralize Schema Definitions: |
| 9 | +Define your Zod schemas in dedicated TypeScript files (e.g., src/lib/schemas/feature-schemas.ts, src/features/my-feature/schemas.ts, or a common schemas.ts if broadly applicable). |
| 10 | +These schema definition files should not include "use client" or "use server" directives, as they are meant to be neutral modules. |
| 11 | +Server Action Implementation ("use server" files): |
| 12 | +Server Action files (marked with "use server") must only export asynchronous functions. |
| 13 | +Import the required Zod schema(s) from your centralized schema file(s) into the Server Action file. |
| 14 | +Use the imported schema for robust server-side validation of any incoming data before processing or database operations. This is a critical security measure. |
| 15 | +Client Component Implementation ("use client" files): |
| 16 | +Import the same Zod schema(s) from your centralized schema file(s) into your Client Components that contain forms. |
| 17 | +Use the schema with a resolver (like zodResolver for react-hook-form) to enable client-side validation, providing immediate feedback to the user. |
| 18 | +When the form is submitted, call the appropriate Server Action, passing the validated (or raw) form data. |
| 19 | +Rationale: |
| 20 | +DRY (Don't Repeat Yourself): Maintains a single source of truth for validation logic, reducing redundancy and potential inconsistencies. |
| 21 | +Next.js Constraints: Adheres to the Next.js App Router constraint that "use server" files can only export async functions. Objects like Zod schemas cannot be directly exported from these files. |
| 22 | +Security: Ensures that all data is re-validated on the server, even if client-side validation is in place, as client-side checks can be bypassed. |
| 23 | +User Experience: Provides immediate validation feedback on the client, improving the form-filling experience. |
| 24 | +Example Workflow Outline: |
| 25 | +Schema Definition (feature-schemas.ts): |
| 26 | +Define the schema, e.g., export const myFeatureSchema = z.object({ /* ...schema details... / }); |
| 27 | +Client Component (MyFeatureForm.tsx - 'use client'): |
| 28 | +Import the schema and the server action. |
| 29 | +Set up the form (e.g., react-hook-form) using the schema with its resolver. |
| 30 | +The form's onSubmit handler calls the server action. |
| 31 | +Server Action (my-feature-action.ts - "use server"): |
| 32 | +Mark the file with "use server". |
| 33 | +Import the schema. |
| 34 | +The exported async server action function receives form data, validates it using the schema, and then performs server-side operations. |
0 commit comments