Skip to content

Commit 9fc226f

Browse files
authored
Add drizzle-zod type coercion docs (#470)
1 parent d953f91 commit 9fc226f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/content/docs/zod.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,33 @@ const userInsertSchema = createInsertSchema(users, {
160160
});
161161
```
162162

163+
**Use case: Type coercion**
164+
165+
```ts copy
166+
import { pgTable, timestamp } from 'drizzle-orm/pg-core';
167+
import { createSchemaFactory } from 'drizzle-zod';
168+
import { z } from 'zod';
169+
170+
const users = pgTable('users', {
171+
...,
172+
createdAt: timestamp().notNull()
173+
});
174+
175+
const { createInsertSchema } = createSchemaFactory({
176+
// This configuration will only coerce dates. Set `coerce` to `true` to coerce all data types or specify others
177+
coerce: {
178+
date: true
179+
}
180+
});
181+
182+
const userInsertSchema = createInsertSchema(users);
183+
// The above is the same as this:
184+
const userInsertSchema = z.object({
185+
...,
186+
createdAt: z.coerce.date()
187+
});
188+
```
189+
163190
### Data type reference
164191

165192
```ts

0 commit comments

Comments
 (0)