drizzle-team / drizzle-team/drizzle-orm
[BUG]: drizzle-orm/zod createSchemaFactory breaks refine callback type inference (implicit 'any')
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
1.0.0-rc.4
### What version of `drizzle-kit` are you using?
1.0.0-rc.4
### Other packages
zod@4.4.3, @hono/zod-openapi@1.4.0
### Describe the Bug
**What is the undesired behavior?**
When I use createSchemaFactory({ zodInstance: z }) with an extended zod instance (in my case from @hono/zod-openapi), the refine callback param ends up typed as any instead of the actual zod schema type. TS throws noImplicitAny (ts 7006) because of this.
**What are the steps to reproduce it?**
```ts
import { sql } from "drizzle-orm"
import { sqliteTable, text } from "drizzle-orm/sqlite-core"
import { createSchemaFactory } from "drizzle-orm/zod"
import { z } from "@hono/zod-openapi"
export const project = sqliteTable("project", {
id: text("id").primaryKey(),
title: text("title").notNull(),
// ...other columns
})
const { createInsertSchema } = createSchemaFactory({ zodInstance: z })
export const projectInsertSchema = createInsertSchema(project, {
title: (schema) => schema.min(1).max(200)
// ^ Parameter 'schema' implicitly has an 'any' type. ts(7006)
})
```
This is literally the "Using an extended Zod instance" example from the docs (https://orm.drizzle.team/docs/zod), I just followed it exactly as written, not sure why it's not working on my end.
I also tried it without the factory, just using createInsertSchema directly, and it works fine there:
```ts
import { createInsertSchema } from "drizzle-orm/zod" // no factory
export const projectInsertSchema = createInsertSchema(project, {
title: (schema) => schema.min(1).max(200) // this one's typed correctly
})
```
So it seems like createSchemaFactory specifically is not passing the zodInstance type down into the refine callback correctly.
Also checked, ran `bun pm ls --all | grep zod` and only one `zod@4.4.3` shows up, so it's not duplicate/conflicting zod versions causing this. Tried it again on a totally fresh install too (new project, default tsconfig, no monorepo setup), same versions, and got the exact same error. So it doesn't seem to be an issue with my setup.
**What is the desired result?**
schema in the refine callback should be typed properly (matching whatever zodInstance I passed in), same as what's shown in the docs.
Contributor guide
Research direction
Start with the createSchemaFactory and createInsertSchema entry points in drizzle-orm/zod, then reproduce the provided TypeScript example using @hono/zod-openapi under noImplicitAny. Done means the refine callback parameter is inferred as the supplied extended Zod schema type without TS7006, while direct createInsertSchema typing remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sqlite, typescript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100