chrishoermann/zod-prisma-types

[BUG] Error during the zod schema generation

Opened this issue · 5 comments

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
}

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

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

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.

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.

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