drizzle-team / drizzle-team/drizzle-orm

[BUG]: drizzle-zod doesn't respect `.json().$type<>()`

Open
#2,855 6 comments 33 reactions 0 assignees View on GitHub
bug drizzle/zod
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.33.0

### What version of `drizzle-kit` are you using?

0.24.2

### Describe the Bug

When I create a JSON field like this `field1: json('field1').$type().notNull().default([])` and create a Zod schema from that table with `createInsertSchema()`, I get errors on data types when I parse the schema into `params` from a request to insert it with Drizzle.

Example:

```ts
const someTable = pgTable('someTable', {
field1: json('field1').$type().notNull().default([]),
// createdAt, updatedAt ...
})

const someThingSchema = createInsertSchema(someTable)
.omit({ createdAt: true, updatedAt: true })
.partial()

/// ..

const params = someThingSchema.parse(await request.json())
const [item] = await drizzleDbInstance.insert(someTable).values(params).returning()`
```

I get typing errors which breaks the build and I have to manually cast the proper type inferred from the table schema again.
And same happens with custom types as well. Removing `.partial()` doesn't really affect the result.

```
...
Types of property 'field1' are incompatible.
Type 'Json | undefined' is not assignable to type 'string[] | SQL | Placeholder | undefined'.
Type 'null' is not assignable to type 'string[] | SQL | Placeholder | undefined'.
```

### Expected behavior

I expect JSON data types are properly inferred from the schema definition.
Maybe I misuse something? I suppose there should also be something like `createUpdateSchema` maybe?

### Environment & setup

"drizzle-zod": "0.5.1".

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.