Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fortezhuo opened this issue Jan 2, 2024 · 4 comments
Assignees
Labels
bug (unconfirmed) Could be a bug

Comments

@fortezhuo
Copy link

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):

  • zod:^3.22.4
  • prisma: ^5.7.1

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

@michiim
Copy link

michiim commented Jan 23, 2024

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
Copy link
Author

fortezhuo commented Jan 24, 2024

@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
Copy link

michiim commented Jan 24, 2024

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug (unconfirmed) Could be a bug
Projects
None yet
Development

No branches or pull requests

3 participants