Skip to content

fix(v4): don't throw from safeParse on bigint multipleOf(0n) - #6466

Merged
colinhacks merged 3 commits into
mainfrom
fix/bigint-multipleof-zero
Aug 26, 2026
Merged

colinhacks merged 3 commits into
mainfrom
fix/bigint-multipleof-zero

Conversation

@colinhacks

Copy link
Copy Markdown
Owner

Carries #6140 forward. The fork has maintainer edits turned off, so that branch couldn't be updated in place β€” MerlijnW70's commit is preserved here with authorship intact.

A bigint schema with multipleOf(0n) threw RangeError: Division by zero out of safeParse. The number equivalent already reports a failure instead of throwing, and the runtime check now guards the divisor the same way.

The original fix was incomplete. The compiler emits x % 0n !== 0n, so a compiled schema still threw and the new test failed under compile mode. A zero divisor is now declined at compile time, which turns the node into a runtime island β€” the same way the compiler already declines a NaN comparison bound.

#6126 fixes the same bug but treats 0n as a valid multiple of 0n, which the number branch rejects.

Costs 6 B gzipped on a classic bundle and nothing on mini; the compiler change tree-shakes out of both.

MerlijnW70 and others added 2 commits August 25, 2026 16:30
The bigint branch of the multiple_of check computed `value % def.value`
directly. When the divisor is `0n`, `value % 0n` throws
`RangeError: Division by zero`, which escapes `safeParse` β€” violating its
contract that it never throws. The number branch already handles a 0 divisor
gracefully: `floatSafeRemainder(value, 0)` is `NaN`, which `!== 0`, so the value
is reported as not-a-multiple and `safeParse` returns `{ success: false }`.

Guard the bigint branch so a `0n` divisor is treated as not-a-multiple too,
making the two branches consistent and keeping `safeParse` throw-free.

Adds a regression test asserting z.bigint().multipleOf(0n).safeParse(10n) returns
success:false, matching z.number().multipleOf(0).
The runtime guard alone left the fast path throwing: compile emits
`x % 0n !== 0n`, so a compiled `z.bigint().multipleOf(0n)` still raised
RangeError out of safeParse and the new test failed under compile mode.
Refuse to compile a zero divisor so the node becomes a runtime island,
matching how the compiler already declines a NaN comparison bound.
Copilot AI lite review requested due to automatic review settings August 25, 2026 23:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No correctness issues found. One optional test suggestion inline.

Reviewed changes β€” the full two-commit range 9d5b20ef..2d386016, covering the runtime check, the compiler refusal, and the regression test.

  • Runtime guard in $ZodCheckMultipleOf β€” the bigint branch short-circuits on a 0n divisor before the modulo, so safeParse reports not_multiple_of instead of letting RangeError: Division by zero escape.
  • Compiler declines a zero divisor β€” generateMultipleOfCheck throws ZodCompileUnsupportedError rather than emitting x % 0n !== 0n, mirroring how comparisonOperand already refuses a NaN bound. The error is islandable, so a nested schema keeps the surrounding structure compiled.
  • Regression test in bigint.test.ts β€” covers the bare schema, 0n as input, parity with z.number().multipleOf(0), and a zero divisor nested in an object.

ℹ️ Verification notes

The semantic choice β€” nothing is a multiple of zero, including 0n itself β€” is the right one, and expect(schema.safeParse(BigInt(0)).success).toBe(false) is exactly the assertion that separates this from #6126's approach. floatSafeRemainder(0, 0) is NaN, so the number branch already rejects it.

I confirmed the compiler commit is load-bearing rather than belt-and-braces: reverting compile.ts to 4d070a03 and re-running the compile-mode project fails with RangeError: Division by zero raised from the generated function. The global compile shim wraps only the compile(inst) call, not the compiled function's invocation, so a parse-time throw would reach user code.

I also traced the paths that could route around the new guard and found none β€” generateChecks is reached uniformly from generateCheck for every schema type, generateLazyCheck always defers to the runtime, fromJSONSchema never builds a bigint multiple_of, and the backward/async directions skip the fast path entirely.

BigInt(0) over the 0n literal matches the surrounding source and this test file, so no note there. Both #6140 and #6126 are already closed.

Pullfrog  | Fix all βž” | Fix πŸ‘s βž” | View workflow run | Using Claude Opus | 𝕏

Comment thread packages/zod/src/v4/core/compile.ts Resolved
The compile-mode re-run of bigint.test.ts covered the behavior end to end
but nothing asserted that codegen declines. Pin it in the error-taxonomy
test next to the NaN-bound analog.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

βœ… No new issues found.

Reviewed changes β€” the single new commit 7af5df61 since the prior review at 2d386016, which takes the one test suggestion left open there.

  • Direct assertion that codegen declines a zero divisor β€” compile(z.bigint().multipleOf(BigInt(0))) is now asserted to throw ZodCompileUnsupportedError in the unsupported features throw ZodCompileUnsupportedError, not raw errors taxonomy test, one line below the z.number().gt(Number.NaN) analog it mirrors.

ℹ️ Verification notes

I checked the new assertion can actually fail rather than riding along on behavior the runtime guard already provides. Deleting the two guard lines from generateMultipleOfCheck and re-running the test fails with expected function to throw an error, but it didn't on exactly that line, so it pins the compiler refusal specifically. Restored afterwards; the working tree is clean. The full test passes under the zod, compile-mode, and typecheck projects.

ZodCompileUnsupportedError propagates out of a top-level compile() call rather than islanding, which is what makes the toThrow form correct here.

The prior comment's optional second half β€” also asserting the islanded form β€” is reasonably skipped: bigint.test.ts already covers a zero divisor nested in an object end-to-end, and the compile-mode project re-runs it.

Pullfrog  | View workflow run | Using Claude Opus | 𝕏

@colinhacks
colinhacks merged commit 97edaf7 into main Aug 26, 2026
11 checks passed
@colinhacks
colinhacks deleted the fix/bigint-multipleof-zero branch August 26, 2026 00:08

This branch was successfully deployed

1 active deployment
Preview – zod-v4 β€” 7af5df61 Deployed Aug 25, 2026 by vercel[bot]
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 this pull request may close these issues.

3 participants