risen228 / nestjs-zod-prisma

Zod Prisma fork for nestjs-zod
MIT License
62 stars 11 forks source link

clashing interpretation of enums #18

Open oldo opened 10 months ago

oldo commented 10 months ago

Hi there, Have been using this package for a while now and it has saved me a lot of time! Thank you. One issue that I have been regularly facing is that enums from Prisma's generated client are of the form:

/**
 * Enums
 */

export const Role: {
  admin: 'admin',
  customer: 'customer'
};

export type Role = (typeof Role)[keyof typeof Role]

However nestjs-zod-prisma exports actual TS enums:

export enum Role {
  admin = "admin",
  customer = "customer"
}

I prefer that actual enums are exported, but often find that I have issues when I use the types/schemas generated by Prisma client and nestjs-zod-prisma interchangeably:

Type 'import("/Users/oldo/node_modules/.prisma/client/index").Role' is not assignable to type 'import("/Users/oldo/packages/zod/src/enums").Role'.
Type '"admin"' is not assignable to type 'Role'.

Would it be possible to generate enums in the same way as Prisma client does? As far as I can tell, there is no way to modify the way Prisma client generates enums...

mohrazzak commented 10 months ago

did you fix it ?

stilyng94 commented 8 months ago

I just wanted to let you know that the below works for me.

/**
 * schema.prisma
 */

model Dispute {
  id               String        @id
  status           DisputeStatus @default(pending) ///@z.union([z.literal('pending'),z.literal('resolved')])
  resolutionDetail String        @db.VarChar(250)
}

enum DisputeStatus {
  pending
  resolved
}

Final outcome

export const DisputeSchema = z.object({
  id: z.string(),
  status: z.union([z.literal('pending'), z.literal('resolved')]),
  resolutionDetail: z.string(),
});