chrishoermann / zod-prisma-types

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

Support prisma 5.0.0 #161

Closed ytnobody closed 8 months ago

ytnobody commented 11 months ago

In 3 days ago, prisma 5.0.0 has been released.

https://github.com/prisma/prisma/releases/tag/5.0.0

nordowl commented 11 months ago

Would also love for Prisma 5 to be supported. Using Prisma 5 broke some things in the generated Zod types (some variables being undefined) which made me have to revert back to Prisma 4.16.2.

andenacitelli commented 11 months ago

Leaving a specific error here that I believe Prisma 5.x.x introduces

./prisma/generated/zod/index.ts:556:45
Type error: Cannot find name 'StringFieldRefInputSchema'.

  554 |
  555 | export const StringFilterSchema: z.ZodType<Prisma.StringFilter> = z.object({
> 556 |   equals: z.union([ z.string(),z.lazy(() => StringFieldRefInputSchema) ]).optional(),
      |                                             ^
  557 |   in: z.union([ z.string().array(),z.lazy(() => ListStringFieldRefInputSchema) ]).optional(),
  558 |   notIn: z.union([ z.string().array(),z.lazy(() => ListStringFieldRefInputSchema) ]).optional(),
  559 |   lt: z.union([ z.string(),z.lazy(() => StringFieldRefInputSchema) ]).optional(),

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"
}

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

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

model User {
  // non-user-facing information about a user
  id    String @id @default(uuid()) @map("_id")
  email String @unique

  // Linkage
  Profile Profile?

  // Audit
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Profile {
  // publicly facing information for ANY given user; only generalized attributes are here
  id String @id @default(uuid()) @map("_id")

  // Requested at initial onboarding
  role      UserRoles
  firstName String
  lastName  String
  fullName  String // Computed as `firstName lastName`

  // Users can optionally adjust these at a later point
  bio      String  @default("")
  imageUrl String? // TODO: Implement actual hosting for these on something like Vercel Blob or S3
  school   String? // TODO: Should eventually be a "search first, if you can't find, create your own" system. Or, pull a dataset and have people reach out if their school isn't listed.

  // Athletes can flag certain coaches that have some edit permissions on their profile
  coachIds String[]

  // Linkage
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId String @unique

  // Audit
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model School {
  id       String @id @default(uuid()) @map("_id")
  name     String
  location String

  @@unique([name, location])
}

enum UserRoles {
  ATHLETE
  COACH
  RECRUITER
  DOCTOR
}

Breaks when running w/ Next.js during next build

nordowl commented 11 months ago

Leaving a specific error here that I believe Prisma 5.x.x introduces

This was my experience aswell. I believe all of the XXXXFieldRefInputSchema variables are undefined. I compared the generated types of Prisma 5 and 4.16, and it seems like Prisma 5 generates those union types, which Prisma 4.16 did not.

Prisma 5 equals: z.union([ z.boolean(),z.lazy(() => BooleanFieldRefInputSchema) ]).optional(),

Prisma 4.16 equals: z.boolean().optional(),

renatoaraujoc commented 11 months ago

Any ETA for supporting Prisma 5? :/

chrishoermann commented 11 months ago

I gave it a quick look and the fieldRef feature is used to compare columns in the same table - as stated in the docs

To my understanding this is not possible to implement via zod schemas because, the field refs need direct access to the prisma client instance - which we do not have.

So to make the generator compatible for now I'll filter out all the fieldRefs and just expose the nonFieldRef types as it was prior to prisma 5.0.0. and see if it works.

chrishoermann commented 11 months ago

I removed the 'fieldRef' in the latest version and fixed some other bugs. Can you please see if the problems are fixed on your end too and if there are any errors left related to prisma 5.x?

renatoaraujoc commented 11 months ago

I removed the 'fieldRef' in the latest version and fixed some other bugs. Can you please see if the problems are fixed on your end too and if there are any errors left related to prisma 5.x?

I just updated to your latest version and I'm now getting this.

Screenshot 2023-08-03 at 17 32 13 Screenshot 2023-08-03 at 17 34 56

You fixed the fieldRef thing but introduced some sort of ModelDefaultArgs that do not exist on my Prisma.

I'm using Prisma v5.0.0.

EDIT: Updating to prisma@5.1.1 to see if it fixes it.

EDIT2: Updating to prisma@5.1.1 fixes the Default bug but introduces a new bug: Screenshot 2023-08-03 at 17 41 38 This is the line: Screenshot 2023-08-03 at 17 42 04

@chrishoermann if you absolutelly really need a prisma.schema to debug this, I can send you my prisma file, privatelly since its a private company project.

chrishoermann commented 11 months ago

@renatoaraujoc thanks for the information. have you tried to delete the generated prisma client in the node_modules folder? I had a similar issue once when there were both clients - in your case from v5.0.0 and v5.1.1 - present in the node modules folder. Somehow typescript didn't get the notice and still referred to the old version during compilation. I solved it by deleting the whole node_modules folder and ran the install and generation again. maybe this solves the issue. otherwise please let me know and we'll see if I can solve this without your schema. or maybe just a restart of the TS server works too??

renatoaraujoc commented 11 months ago

@chrishoermann I did a full re-install with removed node_modules and no luck, same error, emptied all caches as well. I'm trying now with a new yarn.lock (backed up the old one). I'll get back to you soon.

EDIT: @chrishoermann same error after emptying node_modules, new yarn.lock etc. Hit me up and we can solve this together if you want.

chrishoermann commented 11 months ago

@renatoaraujoc which version of zod are you on?

renatoaraujoc commented 11 months ago

@renatoaraujoc which version of zod are you on?

zod@^3.21.1, zod@^3.21.4:
  version "3.21.4"
  resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db"
  integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==
chrishoermann commented 11 months ago

@renatoaraujoc are both versions installed? please use 3.21.1 exclusively. there is a known bug with higher versions of zod that needs to be adressed by the zod team. maybe this fixes the issue.

renatoaraujoc commented 11 months ago

@chrishoermann I'll test with exact v3.21.1 soon and will get back to you.

samrocksc commented 11 months ago

I got it to work right with the following packages, I'm using an nx monorepo for this one:

"zod": "3.21.1",
"zod-prisma-types": "^2.7.7",
"@prisma/client": "^5.1.1",
sebpowell commented 11 months ago

@samrocksc worked for me, thank you!

jonsoku-dev commented 10 months ago

package.json

    "zod": "3.21.1",
    "zod-prisma-types": "^2.7.9",
    "@prisma/client": "^5.2.0",
    "prisma": "^5.2.0"

it works for me on nestjs project. i changed strictNullChecks from false to true, strictNullChecks false is the default setting for tsconfig of nestjs.

chrishoermann commented 8 months ago

I'm closing this issue since the latest version of the generator 2.7.13 should work with the latest prisma version 5.4.2.