Skip to content

Commit bbf4d6b

Browse files
committed
fix(isFloat): reject unknown locales instead of building a broken pattern
1 parent a79ff98 commit bbf4d6b

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/lib/isFloat.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { decimal } from './alpha';
55
export default function isFloat(str, options) {
66
assertString(str);
77
options = options || {};
8+
if (options.locale && !(options.locale in decimal)) {
9+
throw new Error(`Invalid locale '${options.locale}'`);
10+
}
811
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
912
if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') {
1013
return false;

test/validators.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4940,6 +4940,19 @@ describe('Validators', () => {
49404940
});
49414941
});
49424942

4943+
it('should error on invalid locale', () => {
4944+
test({
4945+
validator: 'isFloat',
4946+
args: [{ locale: 'is-NOT' }],
4947+
error: [
4948+
'123',
4949+
'123.123',
4950+
'123,123',
4951+
'3undefined5',
4952+
],
4953+
});
4954+
});
4955+
49434956
it('should validate hexadecimal strings', () => {
49444957
test({
49454958
validator: 'isHexadecimal',

0 commit comments

Comments
 (0)