fix(isFloat): use locale decimal separator for min/max comparison#2734
Open
tan7vir wants to merge 1 commit into
Open
fix(isFloat): use locale decimal separator for min/max comparison#2734tan7vir wants to merge 1 commit into
tan7vir wants to merge 1 commit into
Conversation
isFloat builds its format regex from the locale's decimal separator
(decimal[options.locale]), but the numeric value used for the min/max/lt/gt
range checks was parsed with a hardcoded comma replacement:
parseFloat(str.replace(',', '.'))
For locales whose separator is neither '.' nor ',', the replacement was a
no-op, so parseFloat stopped at the separator and dropped the fractional
part. With the Arabic separator U+066B, parseFloat('3٫14') returned 3
instead of 3.14, corrupting every range comparison for the ar and fa-*
locales -- producing both false negatives (valid values rejected) and
false positives (out-of-range values accepted).
Replace the locale's actual decimal separator before parsing. Format
validation and all other locales are unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2734 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2587 2588 +1
Branches 656 657 +1
=========================================
+ Hits 2587 2588 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
What
isFloatvalidates the format of a number against the locale's decimalseparator (
decimal[options.locale]), but the numeric value used for themin/max/lt/gtrange checks was parsed with a hardcoded commareplacement:
That only normalizes
.and,. For locales whose separator is neither ofthose, the
replaceis a no-op, soparseFloatstops at the separator andsilently drops the fractional part.
The Arabic locale (
ar) and the Farsi locales (fa-IR,fa-AF) use٫(U+066B, Arabic decimal separator). So
parseFloat('3٫14'.replace(',', '.'))returns
3, not3.14, and every range comparison for those locales iswrong — in both directions.
Reproduction (against the published
validator)The false-positive case is the concerning one: a
maxused to enforce alimit silently accepts out-of-range input for these locales.
Fix
Replace the locale's actual decimal separator before parsing:
Comma locales (
de-DE,fr-FR, …) and the default.behavior areunchanged; format validation is untouched.
Tests
Added an
isFloatcase for thearlocale withmin/maxbounds intest/validators.test.js. It fails on the old code (the existing localetests only covered format validation, not range checks) and passes with the
fix. Full validators suite passes (
249 passing).