fix(isFloat): reject unknown locales instead of building a broken pattern - #2868
Open
kory-kaai wants to merge 1 commit into
Open
fix(isFloat): reject unknown locales instead of building a broken pattern#2868kory-kaai wants to merge 1 commit into
kory-kaai wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2868 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2599 2601 +2
Branches 658 659 +1
=========================================
+ Hits 2599 2601 +2 ☔ View full report in Codecov by Harness. 🚀 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.
Fixes #2862
Problem
isFloatinterpolates the locale separator directly into a newRegExpwithout checking that the locale exists:When
options.localeis not a key ofdecimal, the lookup yieldsundefined, which stringifies into the pattern as the literal textundefined. The preceding\\produces\undefined, and because\uis not followed by four hex digits the escape degrades to a literalu. The compiled separator becomes the eight-character stringundefined, so an unrecognised locale silently produces a validator that accepts nonsense and rejects ordinary decimals:de-CHis a real BCP 47 tag that is simply not among the keys indecimal, so it is a plausible value to reach production.Fix
Reject an unknown locale before building the pattern:
This matches the existing convention in the library.
isAlpha,isAlphanumeric,isDecimal,isIdentityCard,isLicensePlate,isMobilePhone,isPostalCodeandisTaxIDall throwInvalid locale `${locale}`.isDecimalis the closest precedent because it reads the samedecimaltable from./alphaand already throws, soisFloatwas the outlier.The guard is deliberately conditional on
options.localebeing truthy, so omitting the option keeps using.exactly as before.Tests
Added a focused
should error on invalid localecase forisFloat, following the same shape as the existing cases forisAlpha,isAlphanumericandisDecimal.Verified in the order described in
AGENTS.md:validator.isFloat("123", {"locale":"is-NOT"}) passed but should errornpm testis green: 324 passing, lint clean, statements/lines/functions at 100%Notes on scope
src/andtest/are touched.README.mdunchanged because the sibling validators that throw on an invalid locale do not document that behaviour either, so adding a note only toisFloatwould be inconsistent. Happy to add it here, or across the locale-aware validators in a separate PR, if you would prefer it documented.into matchisDecimalandisAlpharather thanhasOwnProperty. That keeps the change consistent with its siblings; tightening the lookup across all of them felt out of scope for this fix..for an unknown locale is a smaller behavioural change and I can switch it over.