chrishoermann / zod-prisma-types

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

[BUG] Undefined variable `Decimal` using 3.1.5 #212

Closed nordowl closed 7 months ago

nordowl commented 7 months ago

Describe the bug Generating the schemas having a Decimal field in a Prisma model results in an undefined variable Decimal being used.

export const ModelWhereInputSchema: z.ZodType<Prisma.ModelWhereInput> = z.object({
  // other fields...
  chance: z.union([ z.lazy(() => DecimalFilterSchema),z.union([z.number(),z.string(),z.instanceof(Decimal),z.instanceof(Prisma.Decimal),DecimalJsLikeSchema,]).refine((v) => isValidDecimalInput(v), { message: 'Must be a Decimal' }) ]).optional(),
}).strict();

z.instanceof(Decimal) throws an error, because Decimal is undefined.

I was using zod-prisma-types 2.8.1 up until now, where the following was generated:

export const ModelWhereInputSchema: z.ZodType<Prisma.ModelWhereInput> = z.object({
  // other fields...
  chance: z.union([ z.lazy(() => DecimalFilterSchema),z.union([z.number(),z.string(),DecimalJSLikeSchema,]).refine((v) => isValidDecimalInput(v), { message: 'Must be a Decimal' }) ]).optional(),
}).strict();

Package versions (please complete the following information):

Additional context I think the error was introduced with the 3.0 release. Was using 2.8.1 together with Zod and Prisma as listed above, without any errors.

chrishoermann commented 7 months ago

@nordowl sorry my bad. Since v. 3.0.0 the generator checks if decimal.js is installed and if so adds validation for Decimal to the fields. I just forgot a conditional check. I'll release a fix shortly!

chrishoermann commented 7 months ago

@nordowl This should now be fixed in 3.1.6

nordowl commented 7 months ago

Awesome, thank you! Can confirm this is fixed now. I'm closing this issue.