zd
Explore posts from serversDTDrizzle Team
•Created by Cetus 🔛🔝 on 10/26/2023 in #help
drizzle-zod refine enum
I couldn't get schema.<field>.describe to do anything. I wound up with (using @Cetus 🔛🔝's example)
...
role: () => z.enum(['USER', 'ADMIN'], { required_error: "Please select a role" }), //in createInsertSchema
...
But this feels like I'm completely redefining the schema which I don't love. For example, I could do the following and have a mismatch between the schema and table definition.
role: () => z.enum(['USER', 'ADMIN', 'NEW-SUPER-ADMIN'], { required_error: "Please select a role" }),
I'd prefer (or something like)
role: (schema) => schema.role.notNull({ required_error: "Please select a role" }),
Since then the valid values couldn't change.
The above mismatch is removed if you refactor a little.
export const roleTypes = ["USER", "ADMIN"] as const; // defined anywhere
...
role: mysqlEnum("role", roleTypes).notNull(), // in createTable
...
role: () => z.enum(roleTypes, { required_error: "Please select a role" }), // in createInsertSchema
-zd
3 replies