chrishoermann / zod-prisma-types

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

[BUG] Error during the zod schema generation #235

Open pablo-k-neruda opened 5 months ago

pablo-k-neruda commented 5 months ago

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):

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 commented 5 months ago

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

pablo-k-neruda commented 5 months ago

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

chrishoermann commented 5 months ago

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 commented 5 months ago

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 commented 3 months ago

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