Zod prototype mismatch

Even though the Zod object constructor name created by Drizzle is ZodObject, it is not an instanceof ZodObject. This is causing an error in another library I am using that accepts ZOD objects. Since I've got Zod as a peer dependency, it should be expected that drizzle-zod is using that version when it creates a Zod objects? I could be off here. It appears that drizzle-zod has ZOD as a peer dependecy, but drizzle-orm does not?
28 Replies
bloberenober
bloberenober15mo ago
drizzle-orm doesn't use Zod at all only drizzle-zod does one thing that might cause it is a monorepo setup, where you might potentially have different Zod installations in different packages
Cory
Cory15mo ago
I don't have a monorepo Is this something that you guys can fix on your end? Or something I can tell to the package maintainer of the library I am using?
bloberenober
bloberenober15mo ago
Honestly, I'm not sure. All I know is drizzle-zod uses Zod as peer dependency, so maybe the issue is with another library. Which library is that, btw?
Cory
Cory15mo ago
Superforms They check whether it's a zod object via instanceof
bloberenober
bloberenober15mo ago
which one? 🤔
Cory
Cory15mo ago
https://github.com/ciscoheat/sveltekit-superforms I see they have, zod": "3.x" where as drizzle-zod uses "zod": "*" So that could be the issue
bloberenober
bloberenober15mo ago
Well, * just means "any version"
Cory
Cory15mo ago
Yea I am not an expert I just asked chat GPT lol
bloberenober
bloberenober15mo ago
did you install Zod manually? also, which package manager are you using?
Cory
Cory15mo ago
Yes. I am using pnpm
bloberenober
bloberenober15mo ago
you can shed more light into the issue by checking which Zod versions do both drizzle-zod and superforms reference you can do that by trying to open the library contents inside node_modules and following the require('zod') reference in one of the js files once you do, pay attention to the resolved zod file path, as it will contain the resolved Zod version if those references from drizzle-zod and superforms resolve to different paths, that means those libs use different zod instances, hence the error there may be an easier option to check that (via pnpm cli, probably), but I'm not aware of it 😅
Cory
Cory15mo ago
ok i'll check! It they are both using, zod@3.21.4 double checking though Not sure if this matters but some comments, I assume auto generated, in index.js in drizzle-kit reference zod@3.20.6 Seems like they are using same version Yea not sure
bloberenober
bloberenober15mo ago
drizzle-kit doesn't matter, its runtime is separate
Cory
Cory15mo ago
ah okay well yea it appears they are resolving to same locaiton node_modules/.pnpm/zod@3.21.4/node_modules/zod/index for both
bloberenober
bloberenober15mo ago
does it work without superforms? if you do the instanceof check yourself? also, does superforms work without drizzle-zod?
Cory
Cory15mo ago
let me check No it's false so it's nothing to do with superforms
console.log(schema instanceof ZodObject)
console.log(schema instanceof ZodObject)
If you check that with a schema created by drizzle-zod it will return false so it appears it's nothing to do with superforms
bloberenober
bloberenober15mo ago
weird all I'm doing is literally returning z.object() can you try reinstalling node_modules?
Cory
Cory15mo ago
sure Same thing
bloberenober
bloberenober15mo ago
and just to be sure - if you create z.object() yourself, the instanceof check works?
Cory
Cory15mo ago
uh let me check lol correct Here is one difference I notice If I created a zod object and hover
const exampleZod: ZodObject<{
name: z.ZodString;
}, "strip", z.ZodTypeAny, {
name: string;
}, {
name: string;
}>
const exampleZod: ZodObject<{
name: z.ZodString;
}, "strip", z.ZodTypeAny, {
name: string;
}, {
name: string;
}>
I get this : ZodObject<{ Where as if I hover over drizzle-zod created zodobject I get
(alias) const selectMenuSchema: z.ZodObject<{
name: z.ZodString;
}, z.UnknownKeysParam, z.ZodTypeAny, {
...;
}, {
(alias) const selectMenuSchema: z.ZodObject<{
name: z.ZodString;
}, z.UnknownKeysParam, z.ZodTypeAny, {
...;
}, {
notice, : ZodObject<{ vs : z.ZodObject<{
bloberenober
bloberenober15mo ago
that's because you imported z yourself
Cory
Cory15mo ago
ah okay
bloberenober
bloberenober15mo ago
another thing could you verify you use latest versions of drizzle-orm and drizzle-zod? try running pnpm up -i --latest and see if either of them shows up
Cory
Cory15mo ago
sure orm and kit do I updated, same error
bloberenober
bloberenober15mo ago
Well, I'm out of ideas for now. The instanceof check works in a test inside drizzle-zod, but that's expected, since it always uses the same Zod version as the drizzle-zod itself. If you can create a reproduction repo, it might help to resolve this.
Cory
Cory15mo ago
ok Will do done!
Cory
Cory15mo ago
Stack Overflow
TypeScript instanceof not working
I'm having issues using the instanceof operator and it doesn't seem to work. Here is a part of my code: const results = _.map(items, function(item: Goal|Note|Task, index: number) { ...
Cory
Cory15mo ago
Not sure if this helps?