Skip to content
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

[BUG] Optional fields is being generated as nullable #228

Open
vidz1979 opened this issue Dec 30, 2023 · 1 comment
Open

[BUG] Optional fields is being generated as nullable #228

vidz1979 opened this issue Dec 30, 2023 · 1 comment
Assignees
Labels
bug (unconfirmed) Could be a bug

Comments

@vidz1979
Copy link

Describe the bug

My Prisma model is like this:

model Entity {
  id        String   @id @default(cuid())
  field String?
  fieldWorkaround              String? /// @zod.custom.use(z.string().optional())
}

My generated Zod type is like that:

export const EntitySchema = z.object({
  id: z.string().cuid(),
  field: z.string().nullable(),
  fieldWorkaround: z.string().optional().nullable(),
})

It's worth nothing that "String?" is producing a nullable field, which produces a required validation in forms. I need an optional field, which I achieved using a custom modifier.

Package versions (please complete the following information):
"zod": "^3.22.4",
"zod-prisma-types": "^3.1.6"

@chrishoermann
Copy link
Owner

@vidz1979 maybe you can try the option writeNullishInModelTypes this would result in a schema like

export const EntitySchema = z.object({
  id: z.string().cuid(),
  field: z.string().nullish(), // null or undefined
  fieldWorkaround: z.string().optional().nullish(), //null or undefined
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug (unconfirmed) Could be a bug
Projects
None yet
Development

No branches or pull requests

2 participants