chrishoermann / zod-prisma-types

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

[BUG] Generated schemas contain Typescript error #222

Closed Flusinerd closed 6 months ago

Flusinerd commented 6 months ago

Describe the bug After running prisma generate the generated typescript file contains typescript errors that prevent building.

Screenshots If applicable, add screenshots to help explain your problem. image

image

image

image

Package versions (please complete the following information):

Additional context My schema:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

generator zod {
  provider = "zod-prisma-types"
  output   = "../../../libs/models/src/lib/prisma"
}

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

model Session {
  session   String   @id
  userId    String   @db.Uuid
  expiresAt DateTime
  createdAt DateTime @default(now())
  user      User     @relation(fields: [userId], references: [id])
}

model User {
  id          String    @id @default(uuid()) @db.Uuid
  email       String    @unique
  password    String
  firstName   String
  lastName    String
  displayName String
  location    String?
  bio         String?
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  Sessions    Session[]
}
Flusinerd commented 6 months ago

https://github.com/Flusinerd/clouts This is the repo url. The generated file is in the libs/models... folder

nordowl commented 6 months ago

Facing the same error using zod: 3.22.3 & prisma: 5.5.2

Types of property 'id' are incompatible.
     Type 'number | IntFieldUpdateOperationsInput | undefined' is not assignable to type 'undefined'.
           Type 'number' is not assignable to type 'undefined'.
nordowl commented 6 months ago

The issue seems to be caused by zod versions higher than 3.21.1. See this issue I found. Changing my zod version to 3.21.1 resolved the error.

chrishoermann commented 6 months ago

@Flusinerd @nordowl yes this is a bug in zod versions higher than 3.21.1. To support the latest version you can use the useTypeAssertions option. But be aware that this circumvents the internal type checking. I use it in my own projects without any bugs so far but it can lead to bugs in the typesystem that are hard to debug

andrewmartin commented 3 months ago

Just wanted to say, thank you for documenting that you have the useTypeAssertions available here. Stumbled on this thread and it helped a ton.