Skip to content

Commit 7ef918d

Browse files
authored
fix: support Zod .extend() and deduplicate required fields (#61)
* fix: support Zod .extend() and deduplicate required fields * docs: add zod .extend example * docs: typo * fix: ts error
1 parent cb99c7d commit 7ef918d

File tree

6 files changed

+907
-94
lines changed

6 files changed

+907
-94
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,51 @@ Factory functions work with any naming convention and support:
784784
- Imported schemas
785785
- Multiple factory patterns in the same project
786786

787+
### Schema Composition with `.extend()`
788+
789+
Zod's `.extend()` method allows you to build upon existing schemas:
790+
791+
```typescript
792+
// src/schemas/user.ts
793+
794+
// Base user schema
795+
export const BaseUserSchema = z.object({
796+
id: z.string().uuid().describe("User ID"),
797+
email: z.string().email().describe("Email address"),
798+
});
799+
800+
// Extend with additional fields
801+
export const UserProfileSchema = BaseUserSchema.extend({
802+
name: z.string().describe("Full name"),
803+
bio: z.string().optional().describe("User biography"),
804+
});
805+
806+
// Multiple levels of extension
807+
export const AdminUserSchema = UserProfileSchema.extend({
808+
role: z.enum(["admin", "moderator"]).describe("Admin role"),
809+
permissions: z.array(z.string()).describe("Permission list"),
810+
});
811+
812+
export const UserIdParams = z.object({
813+
id: z.string().uuid().describe("User ID"),
814+
});
815+
816+
// src/app/api/users/[id]/route.ts
817+
818+
/**
819+
* Get user profile
820+
* @pathParams UserIdParams
821+
* @response UserProfileSchema
822+
* @openapi
823+
*/
824+
export async function GET(
825+
request: NextRequest,
826+
{ params }: { params: { id: string } }
827+
) {
828+
// Returns: { id, email, name, bio? }
829+
}
830+
```
831+
787832
### Drizzle-Zod Support
788833

789834
The library fully supports **drizzle-zod** for generating Zod schemas from Drizzle ORM table definitions. This provides a single source of truth for your database schema, validation, and API documentation.

0 commit comments

Comments
 (0)