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] Error during the zod schema generation #235

Open
pablo-k-neruda opened this issue Jan 9, 2024 · 5 comments
Open

[BUG] Error during the zod schema generation #235

pablo-k-neruda opened this issue Jan 9, 2024 · 5 comments
Assignees
Labels
bug (unconfirmed) Could be a bug

Comments

@pablo-k-neruda
Copy link

Describe the bug
Getting the following error - Type 'string' is not assignable to type 'undefined'. when generating the zod schema from prisma

Screenshots
image

Package versions (please complete the following information):

  • zod: 3.22.4
  • prisma: 5.7.1
  • zod-prisma-types: 3.1.6

Additional context
I am using next.js and Supabase with prisma and trying to add Zod in the project. The prisma schema usage works fine in app. Here is the prisma schema. I am using "prisma generate --no-engine".

generator client {
provider = "prisma-client-js"
}

generator zod {
provider = "zod-prisma-types"
}

generator erd {
provider = "prisma-erd-generator"
disabled = true
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model User {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime @default(now()) @db.Timestamptz(6)
email String @unique
password String
status UserStatus @default(PENDING)
profile Profile?
role UserRole @default(ADMIN)
type UserType @default(CUSTOMER)
survey Survey?
}
model Profile {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
user User? @relation(fields: [userId], references: [id])
userId String @db.Uuid @unique
}

model Survey {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
user User? @relation(fields: [userId], references: [id])
userId String? @db.Uuid @unique
question1 String?
question2 String?
}

enum UserStatus {
PENDING
VERIFIED
}

enum UserRole {
USER
ADMIN
}

enum UserType {
CUSTOMER
INTERNAL
}

@pablo-k-neruda
Copy link
Author

Here is the generated file attached (renamed to add .json)
index.ts.json

@pablo-k-neruda
Copy link
Author

Downgrading Zod to 3.21.1 didn't work for me but adding usetypeassertions flag worked.

@chrishoermann
Copy link
Owner

please try setting the following options in your tsconfig.json:

strict: true,
strictNullChecks: true,

this should fix the issue.

the useTypeAssertions flag does also work but was intended as a solution for the bug with zod versions.

@bngarren
Copy link

Had same problem. Prisma v5.6.0, Zod v3.22.4, zod-prisma-types v3.1.6. Tsconfig has strict and strictnullchecks as true, didn't work. Only using 'useTypeAssertions' flag at least made the tsc errors go away.

@goern
Copy link

goern commented Mar 7, 2024

I think createRelationValuesTypes was key for me, here is the stuff I did: https://codeberg.org/feeldata/feeldata.app/commit/21beabe1cd6a92f9f9ad7c0b0f46ed3beeaa762f

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

4 participants