drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Variable created by drizzle-zod is not an instanceof ZodObject

Open saturnonearth opened this issue 1 year ago • 7 comments

What version of drizzle-orm are you using?

0.24.0 for drizzle-orm, and 0.3.0 for drizzle-zod

What version of drizzle-kit are you using?

No response

Describe the Bug

When checking whether the object created by drizzle-zod is an instanceof ZodObject, it returns false.

Expected behavior

It should return True.

Environment & setup

I have setup a basic repo for a SvelteKit project with a page to test what I mean.

https://github.com/saturnonearth/drizzle-zod-bug

  1. Install repo and depencies
  2. pnpm run dev --open
  3. See output in browser
  4. File where test is being run is /src/routes/+page.svelte

saturnonearth avatar Apr 17 '23 23:04 saturnonearth

image image

dhruvsaxena1998 avatar Apr 18 '23 19:04 dhruvsaxena1998

@dhruvsaxena1998 Probably, in your case it's better to use safeParse. I tried to reproduce this in node project without svelte-kit, and this issue has gone.

MattMsh avatar Apr 18 '23 23:04 MattMsh

I tried to reproduce this in node project without svelte-kit, and this issue has gone.

Interesting, so you think it's a SvelteKit bug?

saturnonearth avatar Apr 19 '23 02:04 saturnonearth

I tried to reproduce this in node project without svelte-kit, and this issue has gone.

Interesting, so you think it's a SvelteKit bug?

Probably. Svelte imports ZodType from node_modules/zod/lib/index.mjs file, but createInsertSchema loads ZodType from node_modules/zod/lib/types.js. Same with ZodError. You can check prototypes with Object.getPrototypeOf(). There are classes from different files.

image

MattMsh avatar Apr 19 '23 04:04 MattMsh

Thanks for investigating this @MattMsh! I don't think it's a SvelteKit bug, but rather missing ESM support on drizzle-zod/drizzle-orm side - it seems like your projects run in ESM mode, but Drizzle is currently only built in CJS mode, so it always imports the CJS versions of its dependencies, including Zod. So this issue should be resolved on its own once proper ESM support is implemented for our libraries, which is tracked here: https://github.com/drizzle-team/drizzle-orm/issues/163

dankochetov avatar Apr 19 '23 16:04 dankochetov

@MattMsh I tried to use safeParse

export const insertUserSchema = createInsertSchema(users, {
  email: (schema) => schema.email.email(),
});
const { success, data, error } = insertUserSchema.safeParse(body);

Facing weird issues. image

dhruvsaxena1998 avatar Apr 20 '23 03:04 dhruvsaxena1998

@MattMsh I tried to use safeParse

export const insertUserSchema = createInsertSchema(users, {
  email: (schema) => schema.email.email(),
});
const { success, data, error } = insertUserSchema.safeParse(body);

Facing weird issues. image

Before destructurization, you must check success. Look at documentation

MattMsh avatar Apr 20 '23 06:04 MattMsh

This should now be fixed, because the ESM support was implemented.

dankochetov avatar May 28 '23 22:05 dankochetov