From ab967e2a64cd3cd63aa863920b6e232a344d1a4e Mon Sep 17 00:00:00 2001 From: yfwmaniish Date: Sun, 6 Sep 2026 22:58:40 +0530 Subject: [PATCH] fix(isFloat): fall back to '.' separator for an unknown locale --- src/lib/isFloat.js | 5 ++++- test/validators.test.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/isFloat.js b/src/lib/isFloat.js index 84bdc782c..5eeb9f511 100644 --- a/src/lib/isFloat.js +++ b/src/lib/isFloat.js @@ -5,7 +5,10 @@ import { decimal } from './alpha'; export default function isFloat(str, options) { assertString(str); options = options || {}; - const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`); + // Fall back to `.` when the locale is unknown; otherwise `decimal[locale]` + // is `undefined` and stringifies into the pattern as the literal `undefined`. + const decimalSeparator = decimal[options.locale] || '.'; + const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${decimalSeparator}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`); if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') { return false; } diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..035611dc6 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4857,6 +4857,23 @@ describe('Validators', () => { 'foo', ], }); + test({ + validator: 'isFloat', + args: [{ + locale: 'is-NOT-a-locale', + }], + valid: [ + '123', + '123.123', + '-3.5e2', + ], + invalid: [ + '3undefined5', + '123,123', + 'foo', + '', + ], + }); test({ validator: 'isFloat', args: [{