Skip to content

[BUG]: pgEnum enumValues does not return as literal value anymore #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Gerbuuun opened this issue Apr 1, 2023 · 5 comments
Closed

[BUG]: pgEnum enumValues does not return as literal value anymore #358

Gerbuuun opened this issue Apr 1, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@Gerbuuun
Copy link

Gerbuuun commented Apr 1, 2023

What version of drizzle-orm are you using?

0.23.4

Describe the Bug

import { z } from "zod";
import { pgEnum } from "drizzle-orm/pg-core";

export const sexEnum = pgEnum("sex", ["female", "male", "other"]);

const enumSchema = z.enum(sexEnum.enumValues);

The z.enum() requires a literal value and pgEnum.enumValues used to return as type ["female", "male", "other"].
Now the type is just a string[].
Is this intentional and do I need to change the way I do it?

@Gerbuuun Gerbuuun added the bug Something isn't working label Apr 1, 2023
@dankochetov
Copy link
Contributor

Yes, this was intentional, but seems like it was implemented incorrectly. As a quick workaround, you can extract your values tuple into a variable and pass it both to pgEnum and z.enum.

@dankochetov
Copy link
Contributor

Also, you can use drizzle-zod to create a Zod schema from a table.

@Gerbuuun
Copy link
Author

Gerbuuun commented Apr 2, 2023

Yes I have looked into that. The problem I have with that is that I still need to refine the validation and then add a transformation for sanitization etc. so I still have to do the same kind of work.

Example without drizzle-zod:

// Only the fields that can be changed after insertion
const memberInput = z.object({
  userId: z.string().cuid2(),
  firstName: z.string().max(255),
  middleName: z.string().max(255).nullable(),
  insertion: z.string().max(255).nullable(),
  lastName: z.string().max(255),
  sex: z.enum(["female", "male", "other"]),
  dateOfBirth: z.date().max(new Date()),
});

export const createMemberSchema = memberInput.transform((member) => ({
  id: createId(),
  userId: member.userId,
  firstName: member.firstName.trim(),
  middleName: member.middleName?.trim() || null,
  insertion: member.insertion?.trim() || null,
  lastName: member.lastName.trim(),
  sex: member.sex,
  dateOfBirth: member.dateOfBirth,
  createdAt: new Date(),
  updatedAt: new Date(),
}));

export const updateMemberSchema = memberInput.partial().transform((member) => ({
  userId: member.userId,
  firstName: member.firstName?.trim(),
  middleName: member.middleName?.trim(),
  insertion: member.insertion?.trim(),
  lastName: member.lastName?.trim(),
  sex: member.sex,
  dateOfBirth: member.dateOfBirth,
  updatedAt: new Date(),
}));

Example with drizzle-zod:

const memberInsertSchema = createInsertSchema(members, {
  userId: z.string().cuid2(),
}).transform((member) => ({
  id: createId(),
  userId: member.userId,
  firstName: member.firstName.trim(),
  middleName: member.middleName?.trim() || null,
  insertion: member.insertion?.trim() || null,
  lastName: member.lastName.trim(),
  sex: member.sex,
  dateOfBirth: member.dateOfBirth,
  createdAt: new Date(),
  updatedAt: new Date(),
}));

const memberUpdateSchema = createInsertSchema(members, {
  userId: z.string().cuid2(),
})
  .omit({
    id: true,
    createdAt: true,
    updatedAt: true,
  })
  .partial()
  .transform((member) => ({
    userId: member.userId,
    firstName: member.firstName?.trim(),
    middleName: member.middleName?.trim(),
    insertion: member.insertion?.trim(),
    lastName: member.lastName?.trim(),
    sex: member.sex,
    dateOfBirth: member.dateOfBirth,
    updatedAt: new Date(),
  }));

Of course with drizzle-zod there is a connection between my database table and zod schema. However I lose that anyways when I transform the data.

What would be amazing is to be able to create database tables from a zod schema.

If you think this is possible and something for this project, I might be able to take a look at it but I've never really contributed to projects like this. If you can point me to where this can be implemented, I might try something.

dankochetov added a commit that referenced this issue Apr 5, 2023
- 🐛 Fixed referencing the selected aliased field in the same query
- 🐛 Fixed decimal column data type in MySQL
- 🐛 Fixed mode autocompletion for integer column in SQLite
- 🐛 Fixed extra parentheses in the generated SQL for the `IN` operator (#382)
- 🐛 Fixed regression in `pgEnum.enumValues` type (#358)
- 🎉 Allowed readonly arrays to be passed to `pgEnum`
@dankochetov
Copy link
Contributor

Fixed in 0.23.6

@lontrr
Copy link

lontrr commented Jan 9, 2025

Hello, I'm on 0.38.3 and this isn't fixed. Fields that have pgEnum type return a typescript string, why do they not return the enum values?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants