zod-prisma-types icon indicating copy to clipboard operation
zod-prisma-types copied to clipboard

[BUG] Error during the zod schema generation

Open pablo-k-neruda opened this issue 1 year ago • 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 }

pablo-k-neruda avatar Jan 09 '24 02:01 pablo-k-neruda

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

pablo-k-neruda avatar Jan 09 '24 03:01 pablo-k-neruda

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

pablo-k-neruda avatar Jan 09 '24 04:01 pablo-k-neruda

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.

chrishoermann avatar Jan 09 '24 09:01 chrishoermann

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.

bngarren avatar Jan 10 '24 18:01 bngarren

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

goern avatar Mar 07 '24 15:03 goern