P
Prisma3mo ago
Mgs

Recommended Enum usage with Prisma and Zod

Currently, I have enums in my Prisma models which are also redefined in Zod schemas like this:
export const ItemRequestStatusEnum = z.nativeEnum(ItemRequestStatus);
export type ItemRequestStatusType = z.infer<typeof ItemRequestStatusEnum>;
export const ItemRequestStatusEnum = z.nativeEnum(ItemRequestStatus);
export type ItemRequestStatusType = z.infer<typeof ItemRequestStatusEnum>;
Then, I may reference the type in my code like this:
interface ItemRequestTableProps {
statusFilter: ItemRequestStatusType;
}
interface ItemRequestTableProps {
statusFilter: ItemRequestStatusType;
}
I've started wondering if I should remove the Zod enum/type and simply reference the ItemRequestStatus enum generated by Prisma. Is one approach recommended over the other?
3 Replies
Prisma AI Help
Prisma AI Help3mo ago
You selected to wait for the human sages. They'll share their wisdom soon. Grab some tea while you wait, or check out #ask-ai if you'd like a quick chat with the bot anyway!
Mgs
MgsOP3mo ago
On a related note, does it make any difference if I'm importing the enums from "generated/prisma/client" vs "generated/prisma/enums"?
Nurul
Nurul3mo ago
Hey! If you only need compile time type safety and do not need to validate values at runtime, you can reference the Prisma enum type directly. Related Issue: https://github.com/prisma/prisma/issues/3528 You shouldn't get any issues if you import from generated/prisma/enums

Did you find this page helpful?