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

Bigint with coerce throws exception in safeParse and safeParseAsync #3821

Closed
aleksess opened this issue Oct 28, 2024 · 0 comments · Fixed by #3822
Closed

Bigint with coerce throws exception in safeParse and safeParseAsync #3821

aleksess opened this issue Oct 28, 2024 · 0 comments · Fixed by #3822

Comments

@aleksess
Copy link

Hi,

I noticed a bug while using safeParse and safeParseAsync. Those methods IMO shouldn't throw any errors. Instead, they should hold error in returned object. However, currently this does not work in case of coerced big integers.

Here's an example snippet:

const { z } = require("zod");
const schema = z.object({
  data: z.coerce.bigint(),
});
const example = { data: undefined };
try {
  const result = schema.safeParse(example);
  console.log("RESULT ok?: ", result.success);
} catch (err) {
  console.error("Catched: ", err);
}

Excepted output
RESULT ok?: false

Actual output

Catched:  TypeError: Cannot convert undefined to a BigInt
    at BigInt (<anonymous>)
    at ZodBigInt._parse (/home/aleksander/projects/zod-bigint-error/node_modules/zod/lib/types.js:1205:26)
    at ZodObject._parse (/home/aleksander/projects/zod-bigint-error/node_modules/zod/lib/types.js:1864:37)
    at ZodObject._parseSync (/home/aleksander/projects/zod-bigint-error/node_modules/zod/lib/types.js:146:29)
    at ZodObject.safeParse (/home/aleksander/projects/zod-bigint-error/node_modules/zod/lib/types.js:176:29)
    at Object.<anonymous> (/home/aleksander/projects/zod-bigint-error/index.js:10:25)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant