Extracting literal values from Drizzle enums

I'm trying to make a zod enum schema from the values of a Drizzle enum I've created. I've looked through several other posts trying to do the same thing and it's unclear to me how to do this, particularly for mysqlEnum. The most promising way came from inspecting the enums at runtime. I found that I could access the values, just as I wanted, at myEnum.config.enumValues. The only problem is that Typescript declares config protected and I get type errors.
4 Replies
Angelelz
Angelelz16mo ago
Do you want the enum as a type, as a ZodType or as a runtime value?
PapaFinn
PapaFinnOP15mo ago
I’d be curious to know how to do all of the above though I was specifically asking about creating a zod schema you parse with.
Angelelz
Angelelz15mo ago
For mysql it's a little easier than pg. I have a couple enums in my tables and I declare them outside the table like this:
const userRole = ['admin', 'user'] as const // <-- as const here is very important
type UserRole = typeof userRole[number] // <-- You get the type like this
const userRole = ['admin', 'user'] as const // <-- as const here is very important
type UserRole = typeof userRole[number] // <-- You get the type like this
^ That's how you get the runtime value and the type
Angelelz
Angelelz15mo ago
You can see here how to get the ZodType: https://orm.drizzle.team/docs/zod
Usage - DrizzleORM
Drizzle ORM | %s
Want results from more Discord servers?
Add your server