File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -160,6 +160,33 @@ const userInsertSchema = createInsertSchema(users, {
160
160
});
161
161
```
162
162
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
+
163
190
### Data type reference
164
191
165
192
``` ts
You can’t perform that action at this time.
0 commit comments