Enums

Can pgEnum be converted to zod enums?
15 Replies
Dan
Dan2y ago
yes also, you can use drizzle-zod to do that for you
carlos
carlosOP2y ago
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),
});
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),
});
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
providers: z
.array(
z.object({
id: z.string().min(1),
type: z.enum(historyTypeEnum.enumValues),
}),
)
.min(1),
});
Dan
Dan2y ago
you can just do insertHistorySchema.shape.historyType? it's just a Zod object you can use any Zod API on it
carlos
carlosOP2y ago
amazing mate not sure enums are working.. check this i have this schema:
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
providers: z
.array(
z.object({
id: z.number(),
type: insertHistorySchema.shape.type,
}),
)
.min(1),
});
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/[email protected]/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
: /Users/carlos/code/nubi_monorepo/node_modules/.pnpm/[email protected]/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: }
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: }
carlos
carlosOP2y ago
this looks fine not sure whats going on
Dan
Dan2y ago
looks like a bug let me check
carlos
carlosOP2y ago
yeah,,, but the code looks fine
} else if (column instanceof PgEnumColumn) {
type = z.enum(column.enum.enumValues as [string, ...string[]]);
} else if (column instanceof PgEnumColumn) {
type = z.enum(column.enum.enumValues as [string, ...string[]]);
ill try to check why and make a aPR
Dan
Dan2y ago
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
carlos
carlosOP2y ago
great! in the meantime ill just hardcode the values
Dan
Dan2y ago
@charli can you update drizzle-orm and drizzle-zod to latest and try again?
carlos
carlosOP2y ago
i updated to:
- drizzle-orm 0.23.8
+ drizzle-orm 0.23.13
- drizzle-orm 0.23.8
+ drizzle-orm 0.23.13
still same error
carlos
carlosOP2y ago
carlos
carlosOP2y ago
now the enum is converted to string instead of enum
carlos
carlosOP2y ago
also enum values are shown like this
carlos
carlosOP2y ago
upgraded again and now its fixed! thanks man!
Want results from more Discord servers?
Add your server