Skip to content

fix(isFloat): fall back to '.' separator for an unknown locale - #2878

Open
yfwmaniish wants to merge 1 commit into
validatorjs:masterfrom
yfwmaniish:fix-isfloat-unknown-locale
Open

fix(isFloat): fall back to '.' separator for an unknown locale#2878
yfwmaniish wants to merge 1 commit into
validatorjs:masterfrom
yfwmaniish:fix-isfloat-unknown-locale

Conversation

@yfwmaniish

Copy link
Copy Markdown

Closes #2862.

Bug

isFloat interpolates the locale separator straight into a new RegExp:

const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?...`);

When options.locale is not a key of decimal, decimal[options.locale] is undefined, which stringifies into the pattern as the literal text undefined. The preceding \ yields \undefined, and since \u isn't followed by four hex digits it degrades to a literal u, so the "decimal separator" becomes the 8-character string undefined. There's no error and no fallback:

isFloat('3undefined5', { locale: 'no-such' }); // true  ❌
isFloat('3.5',         { locale: 'no-such' }); // false ❌

Fix

Fall back to . when the locale is unknown:

const decimalSeparator = decimal[options.locale] || '.';

Known locales are unaffected (their separator is always . or ,, both truthy), and the no-locale path already resolved to ..

Tests

Added an unknown-locale block asserting ordinary decimals validate and the undefined-separator strings no longer pass. Verified known-locale behavior (en-US ., de-DE ,) is unchanged.

Copilot AI lite review requested due to automatic review settings September 6, 2026 17:25

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.

@codecov

codecov Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a79ff98) to head (ab967e2).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2878   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2599      2600    +1     
  Branches       658       658           
=========================================
+ Hits          2599      2600    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

isFloat builds a literal undefined separator for an unknown locale

2 participants