chrishoermann / zod-prisma-types

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

[BUG] Generated Schema with Decimal type needs more than just Prisma type import #211

Closed cimchd closed 7 months ago

cimchd commented 7 months ago

Describe the bug In a Prisma Schema with a Decimal field:

model Article {
   id             String          @id @unique @default(cuid())
   name           String          @unique
   price          Decimal?        @db.Decimal(12, 2)
}

The generated zod schema imports Prisma as a type but uses Prisma.Decimal for instanceof where it has to be the actual class and not the type.

// generated/zod/index.ts
import { z } from 'zod';
import type { Prisma } from '../../../prisma/generated/client';

//[...]

/////////////////////////////////////////
// ARTICLE SCHEMA
/////////////////////////////////////////

export const ArticleSchema = z.object({
  id: z.string().cuid(),
  name: z.string(),
  price: z.instanceof(Prisma.Decimal, { message: "Field 'price' must be a Decimal. Location: ['Models', 'App']"}).nullable(),
})

export type Article = z.infer<typeof ArticleSchema>

Screenshots image

Package versions (please complete the following information):

{
    "@prisma/client": "^5.5.2",
    "prisma": "^5.5.2",
    "zod": "^3.22.4",
    "zod-prisma-types": "^3.1.4"
}

Additional context This error occured after upgrading form version 2.8.1 to 3.1.4

chrishoermann commented 7 months ago

@cimchd thanks for pointing this out. I missed to update the imports in singleFileMode. Should work now in latest version.