chrishoermann / zod-prisma-types

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

Decimal type field is not correctly generated or inferred #178

Closed nordowl closed 10 months ago

nordowl commented 10 months ago

On one of my Prisma models I'm using a decimal field myfield Decimal @db.Decimal(20, 5). I redacted most fields because I'd rather not hint at what I'm building.

Screenshot_2

zod-prisma-types generates this Zod type from that field: z.union([z.number(),z.string(),DecimalJSLikeSchema,]).refine((v) => isValidDecimalInput(v), { message: "Field 'myfield' must be a Decimal. Location: [redacted]", })

Screenshot_1

If I hover over the whole Zod schema object generated for that model, Intellisense shows me this type for that field: z.ZodEffects<z.ZodUnion<[z.ZodNumber, z.ZodString, z.ZodType<Prisma.DecimalJsLike, z.ZodTypeDef, Prisma.DecimalJsLike>]>, any, any>

However, when the type is exported like this export type MyType = z.infer<typeof MyTypeSchema>, the property on MyType suddenly becomes optional and is of type any. This means you lose type safety for that property.

Screenshot_3

Occurred using "zod-prisma-types": "^2.7.9", "zod": "^3.22.0", and "prisma": "^5.1.1",. Also tested with "zod-prisma-types": "^2.7.7", "zod": "^3.21.1", and "prisma": "^5.1.1", - same problem.

nordowl commented 10 months ago

Closing this as the error was my fault. The path to the Prisma client changed from node_modules/@prisma/client to node_modules/.prisma/client and I didn't notice. Because of that, Prisma.DecimalJsLike didn't resolve.