Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

clashing interpretation of enums #18

Open
oldo opened this issue Oct 12, 2023 · 2 comments
Open

clashing interpretation of enums #18

oldo opened this issue Oct 12, 2023 · 2 comments

Comments

@oldo
Copy link

oldo commented Oct 12, 2023

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
Copy link

did you fix it ?

@stilyng94
Copy link

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(),
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants