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.
25 Replies
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?
I use this now
export type selectCategorySchema = InferSelectModel<typeof categories>;
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?
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.
Now, I'm doing the same but I can't reproduce
You might need to veify if you're on the latest version of everything
Did you use the CLI?
No
That's exactly why I can now blame the tool you're using lol
It's probably not pulling the latests versions
I have "zod": "^3.22.2" and "drizzle-zod": "^0.5.1",
Me too, what drizzle-orm version do you have?
"drizzle-orm": "^0.28.6",
I can't reproduce. The only way I can help you is with a reproduction repo
I even added some transformation to test but I can't reproduce
found the problem
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
againI did but the error is still there, do I need to delete package.json as well?
Not package.json
Only the lock file
Restart your editor I guess
Still seeing the errors despite deleting package-lock.json, then deleting node_modules & running "bun i"
Try with pnpm
Very strange, it works with npm i drizzle-zod but not bun add drizzle-zod
In my cloned repo it worked fine with bun after I deleted node_modules and any lock files
Thanks! This was driving me nuts for the last couple of weeks. A deletion of node_modules and bundb.lock fixed it.