Repository navigation
fix(v4): don't throw from safeParse on bigint multipleOf(0n) - #6466
Conversation
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.
There was a problem hiding this comment.
βΉοΈ 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 a0ndivisor before the modulo, sosafeParsereportsnot_multiple_ofinstead of lettingRangeError: Division by zeroescape. - Compiler declines a zero divisor β
generateMultipleOfCheckthrowsZodCompileUnsupportedErrorrather than emittingx % 0n !== 0n, mirroring howcomparisonOperandalready 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,0nas input, parity withz.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.
Claude Opus ο½ π
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.
There was a problem hiding this comment.
β 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 throwZodCompileUnsupportedErrorin theunsupported features throw ZodCompileUnsupportedError, not raw errorstaxonomy test, one line below thez.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.
Claude Opus ο½ π

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)threwRangeError: Division by zeroout ofsafeParse. 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
0nas a valid multiple of0n, 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.