chrishoermann / zod-prisma-types

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

[BUG] z.instanceof(Prisma.Decimal) is not playing well with vite build while consume schema for client side #229

Open fortezhuo opened 6 months ago

fortezhuo commented 6 months ago

Describe the bug z.instanceof(Prisma.Decimal) is not playing well with vite build while consume schema for client side

function requireIndexBrowser() {
  if (hasRequiredIndexBrowser)
    return indexBrowser$1;
  hasRequiredIndexBrowser = 1;
  const prisma2 = requireIndexBrowser();
  indexBrowser$1 = prisma2;
  return indexBrowser$1;
}
const prisma = requireIndexBrowser(); // prisma is undefined here
var indexBrowser = prisma;
const DECIMAL_STRING_REGEX$1 = /^(?:-?Infinity|NaN|-?(?:0[bB][01]+(?:\.[01]+)?(?:[pP][-+]?\d+)?|0[oO][0-7]+(?:\.[0-7]+)?(?:[pP][-+]?\d+)?|0[xX][\da-fA-F]+(?:\.[\da-fA-F]+)?(?:[pP][-+]?\d+)?|(?:\d+|\d*\.\d+)(?:[eE][-+]?\d+)?))$/;
const isValidDecimalInput = (v) => {
  if (v === void 0 || v === null)
    return false;
  return typeof v === "object" && "d" in v && "e" in v && "s" in v && "toFixed" in v || typeof v === "string" && DECIMAL_STRING_REGEX$1.test(v) || typeof v === "number";
};
const DecimalJsLikeSchema = z.object({
  d: z.array(z.number()),
  e: z.number(),
  s: z.number(),
  toFixed: z.function(z.tuple([]), z.string())
});
const NestedDecimalFilterSchema = z.object({
  equals: z.union([z.number(), z.string(), z.instanceof(indexBrowser.Prisma.Decimal), DecimalJsLikeSchema]).refine((v) => 
                                                                                            ^^^^^^^^^^^^^^^^^^^^^^ 
isValidDecimalInput(v), { message: "Must be a Decimal" }).optional(),
  in: z.union([z.number().array(), z.string().array(), z.instanceof(indexBrowser.Prisma.Decimal).array(), DecimalJsLikeSchema.array()]).refine((v) => Array.isArray(v) && v.every((v2) => isValidDecimalInput(v2)), { message: "Must be a Decimal" }).optional(),
  notIn: z.union([z.number().array(), z.string().array(), z.instanceof(indexBrowser.Prisma.Decimal).array(), DecimalJsLikeSchema.array()]).refine((v) => Array.isArray(v) && v.every((v2) => isValidDecimalInput(v2)), { message: "Must be a Decimal" }).optional(),
  lt: z.union([z.number(), z.string(), z.instanceof(indexBrowser.Prisma.Decimal), DecimalJsLikeSchema]).refine((v) => isValidDecimalInput(v), { message: "Must be a Decimal" }).optional(),
  lte: z.union([z.number(), z.string(), z.instanceof(indexBrowser.Prisma.Decimal), DecimalJsLikeSchema]).refine((v) => isValidDecimalInput(v), { message: "Must be a Decimal" }).optional(),
  gt: z.union([z.number(), z.string(), z.instanceof(indexBrowser.Prisma.Decimal), DecimalJsLikeSchema]).refine((v) => isValidDecimalInput(v), { message: "Must be a Decimal" }).optional(),
  gte: z.union([z.number(), z.string(), z.instanceof(indexBrowser.Prisma.Decimal), DecimalJsLikeSchema]).refine((v) => isValidDecimalInput(v), { message: "Must be a Decimal" }).optional(),
  not: z.union([z.union([z.number(), z.string(), z.instanceof(indexBrowser.Prisma.Decimal), DecimalJsLikeSchema]).refine((v) => isValidDecimalInput(v), { message: "Must be a Decimal" }), z.lazy(() => NestedDecimalFilterSchema)]).optional()
}).strict();

Screenshots

image

Package versions (please complete the following information):

Temp Workaround I remove all z.instanceof(Prisma.Decimal), and z.instance(Prisma.Decimal).array() using vscode

michiim commented 5 months ago

It would be amazing if you avoided using 'prisma/client' entirely in the schema files to enable their usage on the client side (browser).

fortezhuo commented 5 months ago

@michiim Yeah.. And for temp solution, since the files generated by zod-prisma-types automatically. I only can run simple patch to remove all of "z.instance(Prisma.Decimal)" after.


async function clean(path: string, files: string[]) {
  for await (const file of files) {
    let content = fs.readFileSync(resolve(path, `./${file}`), "utf8")
    if (content.indexOf("z.instanceof(Prisma.Decimal)") >= 0) {
      content = content.replaceAll("z.instanceof(Prisma.Decimal).array(),", "")
      content = content.replaceAll("z.instanceof(Prisma.Decimal),", "")

      fs.writeFile(resolve(path, `./${file}`), content, "utf8", (err) => {
        if (!err) {
          console.log(`File ${path}/${file} patched`)
        }
      })
    }
  }
}
michiim commented 5 months ago

@fortezhuo I don't think you should do that. It would be better if zod-prisma-types avoided using Prisma in the schema files, so that these files can also be used in browsers.

fortezhuo commented 5 months ago

@chrishoermann what do you think if zod-prisma-types avoided using Prisma in the schema files ?

oldo commented 3 weeks ago

This is also an issue for me too. I am trying to import generated schemas into React Native but now I get complaints that Unable to resolve module os.