Enums

Ccharli4/11/2023
Can pgEnum be converted to zod enums?
Bbloberenober4/11/2023
yes
also, you can use drizzle-zod to do that for you
Ccharli4/13/2023
im using drizzle for the schema but i don't know how to do it for the enum
Ccharli4/13/2023
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),
});
Ccharli4/13/2023
ok i think i found how
Ccharli4/13/2023
const insertHistorySchemaWithProviders = insertHistorySchema.extend({
  providers: z
    .array(
      z.object({
        id: z.string().min(1),
        type: z.enum(historyTypeEnum.enumValues),
      }),
    )
    .min(1),
});
Bbloberenober4/13/2023
you can just do insertHistorySchema.shape.historyType?
Bbloberenober4/13/2023
it's just a Zod object
Bbloberenober4/13/2023
you can use any Zod API on it
Ccharli4/13/2023
amazing mate
Ccharli4/13/2023
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),
});


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: }
Ccharli4/13/2023
this looks fine not sure whats going on
Bbloberenober4/14/2023
looks like a bug
Bbloberenober4/14/2023
let me check
Ccharli4/14/2023
yeah,,, but the code looks fine

    } else if (column instanceof PgEnumColumn) {
            type = z.enum(column.enum.enumValues as [string, ...string[]]);
Ccharli4/14/2023
ill try to check why and make a aPR
Bbloberenober4/14/2023
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
Ccharli4/14/2023
great! in the meantime ill just hardcode the values
Bbloberenober4/14/2023
@charli can you update drizzle-orm and drizzle-zod to latest and try again?
Ccharli4/14/2023
i updated to:

- drizzle-orm 0.23.8
+ drizzle-orm 0.23.13
Ccharli4/14/2023
still same error
Ccharli4/15/2023
Ccharli4/15/2023
now the enum is converted to string instead of enum
Ccharli4/15/2023
also enum values are shown like this
Ccharli4/15/2023
upgraded again and now its fixed!
Ccharli4/15/2023
thanks man!