chrishoermann / zod-prisma-types

Generator creates zod types for your prisma models with advanced validation
Other
579 stars 43 forks source link

Remove Type postfix from enum types #108

Open oceandrama opened 1 year ago

oceandrama commented 1 year ago

Now, with createModelTypes=true it generates this for models:

export const UserSchema = z.object({
  role: RoleSchema,
  id: z.number().int(),
  login: z.string(),
  name: z.string(),
  email: z.string().nullable(),
});

export type User = z.infer<typeof UserSchema>;

and this for enums:

export const RoleSchema = z.enum(["ADMIN", "USER"]);
export type RoleType = `${z.infer<typeof RoleSchema>}`;

and if you have enum like SectionType it generates SectionTypeType type

It's look like the lack of consistency to add Type postfix for enums and not to add it to models. It's impossible to create enum and model with the same name inside schema.prisma file, so there will be no name conflict after removing this postfix

chrishoermann commented 1 year ago

@oceandrama I chose to add the Type because if somehow you need to import the enum from prisma client it would be named Role. So to be able to distiguish between the enum and the type I chose to add Type to the name. Maybe it would make more sense to name it something like RoleUnion or RoleKeyUnion?

oceandrama commented 1 year ago

@prisma/client exports enum and its type with the same name:

export const Role: {
  ADMIN: 'ADMIN',
  USER: 'USER'
};

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

So you can import only type from @prisma/client too without zod-prisma-types

import type { Role } from "@prisma/client";
// or
import { type Role } from "@prisma/client";