drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: drizzle-zod apply default column value to zod schema `.default`
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Feature hasn't been suggested before.
- [x] I have verified this feature I'm about to request hasn't been suggested before.
### Describe the enhancement you want to request
When you have `.default` for a `pgTable` column, that value should also apply to the `.default` in the zod schema created from `createSelectSchema`/etc, so that in the resulting schema, the default is applied, ie the resulting schema type has a `z.ZodDefault<...>` wrapper.
For example, currently, when you do this:
```typescript
const thisTable = pgTable('this_table', {
field: boolean('field').default(true).notNull(),
})
const thisTableSchema = createSelectSchema(thisTable)
```
`typeof thisTableSchema` is:
```typescript
z.ZodObject<{
field: z.ZodBoolean;
}>
```
Which indicates the default would never get set when you do `thisTableSchema.parse(...)`
Current solution is to do:
```typescript
const thisTableSchema = createSelectSchema(thisTable, {
field: (s) => s.default(true),
})
```
Which gives you a resulting schema of type:
```typescript
z.ZodObject<{
field: z.ZodDefault;
}>
```
Since we are passing a value for the default in the `pgTable` declaration, it would be nice and make sense if it was applied in the zod schema creation as well, instead of having to set the same default value in both locations.
Contributor guide
Assessment
This issue has not been assessed yet.