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
MarvinKROP16mo 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
Hugo16mo ago
I use this now export type selectCategorySchema = InferSelectModel<typeof categories>;
Angelelz
Angelelz16mo 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
MarvinKROP16mo 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
Angelelz16mo ago
Now, I'm doing the same but I can't reproduce
Angelelz
Angelelz16mo ago
No description
Angelelz
Angelelz16mo ago
No description
Angelelz
Angelelz16mo ago
You might need to veify if you're on the latest version of everything
MarvinKR
MarvinKROP16mo ago
Did you use the CLI?
Angelelz
Angelelz16mo 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
MarvinKROP16mo ago
I have "zod": "^3.22.2" and "drizzle-zod": "^0.5.1",
Angelelz
Angelelz16mo ago
Me too, what drizzle-orm version do you have?
MarvinKR
MarvinKROP16mo ago
"drizzle-orm": "^0.28.6",
Angelelz
Angelelz16mo ago
I can't reproduce. The only way I can help you is with a reproduction repo
Angelelz
Angelelz16mo ago
I even added some transformation to test but I can't reproduce
No description
Angelelz
Angelelz16mo ago
found the problem
Angelelz
Angelelz16mo ago
No description
Angelelz
Angelelz16mo 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
MarvinKROP16mo ago
I did but the error is still there, do I need to delete package.json as well?
Angelelz
Angelelz16mo ago
Not package.json Only the lock file Restart your editor I guess
MarvinKR
MarvinKROP16mo ago
Still seeing the errors despite deleting package-lock.json, then deleting node_modules & running "bun i"
No description
Angelelz
Angelelz16mo ago
Try with pnpm
MarvinKR
MarvinKROP16mo ago
Very strange, it works with npm i drizzle-zod but not bun add drizzle-zod
Angelelz
Angelelz16mo ago
In my cloned repo it worked fine with bun after I deleted node_modules and any lock files
Andrioid
Andrioid15mo 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