Fix: correct multipleOf validation for floating point values#5793
Open
cyphercodes wants to merge 1 commit intocolinhacks:mainfrom
Open
Fix: correct multipleOf validation for floating point values#5793cyphercodes wants to merge 1 commit intocolinhacks:mainfrom
cyphercodes wants to merge 1 commit intocolinhacks:mainfrom
Conversation
…ison
Replace string-manipulation-based floatSafeRemainder with a
tolerance-based ratio comparison that correctly handles floating
point values in scientific notation (e.g. 2.5e-7, 1e-7).
The old approach parsed .toString() representations to count decimal
places, which failed for values like 2.5e-7 where split('.') gives
misleading results. The new approach checks whether val/step is
close to an integer using Number.EPSILON scaled to the ratio magnitude.
Fixes colinhacks#5792
Author
|
Good catch — I audited all call sites and confirmed no code depends on the non-zero magnitude:
These are the only two call sites in the entire codebase (grep confirmed, including tests). Both exclusively check |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
z.number().multipleOf(1e-7).safeParse(2.5e-7)incorrectly returns success. The value2.5e-7is not an integer multiple of1e-7(since2.5e-7 / 1e-7 = 2.5), yet validation passes.Root Cause
The
floatSafeRemainderfunction parses.toString()representations to count decimal places. When values use scientific notation (e.g.2.5e-7),split('.')gives misleading results ("5e-7"with length 3), leading to incorrect integer conversions and a false pass.Solution
Replace the string-manipulation-based approach with a tolerance-based ratio comparison:
This checks whether
val / stepis close to an integer, which is the mathematical definition of "multiple of". The tolerance usesNumber.EPSILONscaled to the ratio magnitude to correctly handle IEEE 754 representation limits at any scale.Changes
packages/zod/src/v4/core/util.ts— RewrotefloatSafeRemainderpackages/zod/src/v3/types.ts— Same fix for v3 backward compatibilitypackages/zod/src/v4/classic/tests/number.test.ts— Added regression test formultipleOfsilently accepts non-multiples for small numbers (scientific notation bug infloatSafeRemainder) #5792Test Results
All 3577 tests pass across 323 test files with 0 type errors.
Closes #5792