chrishoermann / zod-prisma-types

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

Generated types mismatch (Inside NX monorepo lib) #144

Closed d-ivashchuk closed 1 year ago

d-ivashchuk commented 1 year ago

hey @chrishoermann! thanks a lot for building this awesome library - I am now building the dream DX setup with full e2e type-safety, and zod-prisma-types fit well in it.

I experienced some problems with the simple generation, so it might be something I don't understand conceptually here:

I have a very minimalistic setup here, just newly generated lib inside NX monorepo.

Screenshot 2023-05-18 at 09 56 17

My schema is super small as well:

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

generator zod {
  provider = "zod-prisma-types"
  output   = "./zod"
}

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

model Post {
  id          String   @id @default(cuid())
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
  title       String   @db.VarChar(255)
  content     String?
  description String?
  published   Boolean  @default(false)
  tags        String[]
  image       String?
}

Straight after running npx prisma generate I see the zod output which immediately throws some type errors at me. I guess it's not expected at all :/

Screenshot 2023-05-18 at 09 58 22

Maybe my tsconfigs would be of use here:

tsconfig.json

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "module": "commonjs"
  },
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.lib.json"
    }
  ]
}

tsconfig.lib.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "../../dist/out-tsc",
    "declaration": true,
    "types": ["node"]
  },
  "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
  "include": ["src/**/*.ts"]
}

would really appreciate any advice here, I think that zod-prisma-types is awesome addition to my flow and I really want to use it :D

chrishoermann commented 1 year ago

@d-ivashchuk thanks for reaching out. Glad you like the library. There ist a known inkompatibility/bug with zod versions greater than 3.21.1.Maybe this could be the source of the type error?

d-ivashchuk commented 1 year ago

I think I rubber duckked myself into solution. If you look at my tsconfigs - I am missing strict: true. I tried to look at your example and just see what would make sense. It seems far better now!

chrishoermann commented 1 year ago

Another thing that comes to my mind is that in some rare cases the prisma client index.d.ts is not updated when changes in the prisma.schema are made. This could also result in a type error. Furthermore there are some known issues with casing of the typenames as in #95 and #121

chrishoermann commented 1 year ago

@d-ivashchuk did you have any more problems with this issue or is it resolved?

d-ivashchuk commented 1 year ago

No, let's close it! The above mentioned fixes worked