DT
Join ServerDrizzle Team
help
Enums
Can pgEnum be converted to zod enums?
yes
also, you can use drizzle-zod to do that for you
also, you can use drizzle-zod to do that for you
im using drizzle for the schema but i don't know how to do it for the enum
im trying to do this
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
providers: z
.array(
z.object({
id: z.string().min(1),
type: // Enum used in insertHistorySchema,
}),
)
.min(1),
});
ok i think i found how
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
providers: z
.array(
z.object({
id: z.string().min(1),
type: z.enum(historyTypeEnum.enumValues),
}),
)
.min(1),
});
you can just do
insertHistorySchema.shape.historyType
?it's just a Zod object
you can use any Zod API on it
amazing mate
not sure enums are working.. check this i have this schema:
but zod is giving me this error:
if i debug the zod codebase this is what we are sending to zod as
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
providers: z
.array(
z.object({
id: z.number(),
type: insertHistorySchema.shape.type,
}),
)
.min(1),
});
but zod is giving me this error:
: /Users/carlos/code/nubi_monorepo/node_modules/.pnpm/zod@3.21.4/node_modules/zod/lib/types.js:2633
api:dev: if (this._def.values.indexOf(input.data) === -1) {
api:dev: ^
api:dev:
api:dev:
api:dev: TypeError: this._def.values.indexOf is not a function
if i debug the zod codebase this is what we are sending to zod as
this._def.values
:api:dev: this._def.values [Function (anonymous)] {
api:dev: enumName: 'history_type',
api:dev: enumValues: [ 'deposit', 'bail', 'mixed' ],
api:dev: [Symbol(isPgEnum)]: true
api:dev: }
this looks fine not sure whats going on
looks like a bug
let me check
yeah,,, but the code looks fine
} else if (column instanceof PgEnumColumn) {
type = z.enum(column.enum.enumValues as [string, ...string[]]);
ill try to check why and make a aPR
I'm currently reworking the internal API for enums a bit, which affects how they are processed by drizzle-zod, so it'll be easier for me to do this on my own
great! in the meantime ill just hardcode the values
@charli can you update drizzle-orm and drizzle-zod to latest and try again?
i updated to:
- drizzle-orm 0.23.8
+ drizzle-orm 0.23.13
still same error
now the enum is converted to string instead of enum
also enum values are shown like this
upgraded again and now its fixed!
thanks man!