Issue with Drizzle-Zod: Due to a type mismatch between the ZodObject and ZodType types.

I have issues with this part of the code "due to a type mismatch between the ZodObject and ZodType types. The ZodObject type is missing a readonly property that is required by the ZodType type": export type Media = z.infer<typeof updateMediaSchema>; export type NewMedia = z.infer<typeof insertMediaSchema>; export type NewMediaParams = z.infer<typeof insertMediaParams>; export type UpdateMediaParams = z.infer<typeof updateMediaParams>; export type MediaId = z.infer<typeof mediaIdSchema>["id"]; Type 'ZodObject<{ id: ZodNumber; imageUrl: ZodString; listingId: ZodString; updatedAt: ZodDate; createdAt: ZodDate; imageOrder: ZodNumber; }, UnknownKeysParam, ZodTypeAny, { ...; }, { ...; }>' does not satisfy the constraint 'ZodType<any, any, any>'. Property 'readonly' is missing in type 'ZodObject<{ id: ZodNumber; imageUrl: ZodString; listingId: ZodString; updatedAt: ZodDate; createdAt: ZodDate; imageOrder: ZodNumber; }, UnknownKeysParam, ZodTypeAny, { ...; }, { ...; }>' but required in type 'ZodType<any, any, any>'. 'readonly' is declared here.
No description
25 Replies
MarvinKR
MarvinKROP15mo ago
The readonly() method is not available on the ZodObject type. It's available on the ZodType class, which is the base class for all Zod schemas. The createSelectSchema and createInsertSchema functions I'm using to create updateMediaSchema and insertMediaSchema are returning ZodObject instances, not ZodType instances. That's why I'm getting an error when I try to call readonly() on these schemas. Unfortunately, Zod doesn't provide a built-in way to convert a ZodObject to a ZodType. Does anyone got a suggestion on how to fix this? @justhugo saw you had a similar issue using Drizzle-Zod?
Hugo
Hugo15mo ago
I use this now export type selectCategorySchema = InferSelectModel<typeof categories>;
Angelelz
Angelelz15mo ago
I agree with Hugo You are currently going schema -> ZodSchema -> type With Hugo's approach you'll go schema -> type directly. Unless you have a reason to do it this way?
MarvinKR
MarvinKROP15mo ago
I was using @gookerloober Kiramase CLI (https://github.com/nicoalbanese/kirimase) and thought it would work out of the box. But will update it now based on Hugo’s solution. Thanks!
GitHub
GitHub - nicoalbanese/kirimase: Build full-stack Nextjs apps faster
Build full-stack Nextjs apps faster. Contribute to nicoalbanese/kirimase development by creating an account on GitHub.
Angelelz
Angelelz15mo ago
Now, I'm doing the same but I can't reproduce
Angelelz
Angelelz15mo ago
No description
Angelelz
Angelelz15mo ago
No description
Angelelz
Angelelz15mo ago
You might need to veify if you're on the latest version of everything
MarvinKR
MarvinKROP15mo ago
Did you use the CLI?
Angelelz
Angelelz15mo ago
No That's exactly why I can now blame the tool you're using lol It's probably not pulling the latests versions
MarvinKR
MarvinKROP15mo ago
I have "zod": "^3.22.2" and "drizzle-zod": "^0.5.1",
Angelelz
Angelelz15mo ago
Me too, what drizzle-orm version do you have?
MarvinKR
MarvinKROP15mo ago
"drizzle-orm": "^0.28.6",
Angelelz
Angelelz15mo ago
I can't reproduce. The only way I can help you is with a reproduction repo
Angelelz
Angelelz15mo ago
I even added some transformation to test but I can't reproduce
No description
Angelelz
Angelelz15mo ago
found the problem
Angelelz
Angelelz15mo ago
No description
Angelelz
Angelelz15mo ago
Just delete package-lock.json It's causing an issue with your bun installation After you delete, package-lock.json, detele node_modules and run bun i again
MarvinKR
MarvinKROP15mo ago
I did but the error is still there, do I need to delete package.json as well?
Angelelz
Angelelz15mo ago
Not package.json Only the lock file Restart your editor I guess
MarvinKR
MarvinKROP15mo ago
Still seeing the errors despite deleting package-lock.json, then deleting node_modules & running "bun i"
No description
Angelelz
Angelelz15mo ago
Try with pnpm
MarvinKR
MarvinKROP15mo ago
Very strange, it works with npm i drizzle-zod but not bun add drizzle-zod
Angelelz
Angelelz15mo ago
In my cloned repo it worked fine with bun after I deleted node_modules and any lock files
Andrioid
Andrioid14mo ago
Thanks! This was driving me nuts for the last couple of weeks. A deletion of node_modules and bundb.lock fixed it.
Want results from more Discord servers?
Add your server