From 82de6415f4aee85b784221f1c6b7d068cc197d3c Mon Sep 17 00:00:00 2001 From: Theerapat Chawannakul Date: Sat, 10 Oct 2020 17:45:11 +0700 Subject: [PATCH 1/2] Support isNumeric options thousand_separator (Issues: #1433) --- src/lib/isNumeric.js | 9 ++++++- test/validators.js | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/lib/isNumeric.js b/src/lib/isNumeric.js index 4cc7ea5b3..d0569e689 100644 --- a/src/lib/isNumeric.js +++ b/src/lib/isNumeric.js @@ -5,8 +5,15 @@ const numericNoSymbols = /^[0-9]+$/; export default function isNumeric(str, options) { assertString(str); + if (options && options.no_symbols) { return numericNoSymbols.test(str); } - return (new RegExp(`^[+-]?([0-9]*[${(options || {}).locale ? decimal[options.locale] : '.'}])?[0-9]+$`)).test(str); + + const decimal_char = (options || {}).locale ? decimal[options.locale] : '.'; + if (options && options.thousand_separator) { + return new RegExp(`^[+-]?[0-9]{1,3}(${options.thousand_separator}[0-9]{3})*([${decimal_char}][0-9]+)?$`).test(str); + } + + return (new RegExp(`^[+-]?([0-9]*[${decimal_char}])?[0-9]+$`)).test(str); } diff --git a/test/validators.js b/test/validators.js index 519035e1b..354219540 100644 --- a/test/validators.js +++ b/test/validators.js @@ -2016,6 +2016,68 @@ describe('Validators', () => { }); }); + it('should validate numeric strings with thousand separator', () => { + test({ + validator: 'isNumeric', + args: [{ + thousand_separator: '_', + }], + valid: [ + '123', + '0', + '-0', + '+123', + '123_123', + '123.000', + '123_123.000', + '123_123_123.000', + '+123_123_123.000', + '-123_123_123.000', + '+000_000', + ], + invalid: [ + ' ', + '', + ',', + '00123', + '-00123', + '_123', + '_123.000', + ], + }); + }); + + it('should validate numeric strings with thousand separator and locale', () => { + test({ + validator: 'isNumeric', + args: [{ + thousand_separator: '_', + locale: 'fr-FR', + }], + valid: [ + '123,000', + '123_123', + '123_123,000', + '123_123_123,000', + '+123_123_123,000', + '-123_123_123,000', + '+000_000,000', + ], + invalid: [ + ' ', + '', + ',', + '_123', + '_123.000', + '1_23', + '1_23.000', + '1_22_123', + '1_22_123.000', + '123_123_123.000_123', + ], + }); + }); + it('should validate ports', () => { test({ validator: 'isPort', From 45cc2dd2ec517da86a900e6738af5401eb89af17 Mon Sep 17 00:00:00 2001 From: Theerapat Chawannakul Date: Sat, 10 Oct 2020 18:14:13 +0700 Subject: [PATCH 2/2] Rename thousand_separator -> thousands_separator Validate options.thousands_separator value --- README.md | 2 +- src/lib/isNumeric.js | 10 ++++++++-- test/validators.js | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0d334a92b..f6273be56 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Validator | Description **isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,

(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-KE', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-UY', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-RE', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW']` OR defaults to 'any'. If 'any' or a falsey value is used, function will check if any of the locales match).

`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`. **isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid]. **isMultibyte(str)** | check if the string contains one or more multibyte chars. -**isNumeric(str [, options])** | check if the string contains only numbers.

`options` is an object which defaults to `{no_symbols: false}` it also has locale as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).

`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. +**isNumeric(str [, options])** | check if the string contains only numbers.

`options` is an object which defaults to `{no_symbols: false, thousand_separator: ''}` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`). `thousand_separator` can be set using a single character (e.g. `,` or `_`) to support validation of a number with a thousand separator.

`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. **isOctal(str)** | check if the string is a valid octal number. **isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'MT', 'NL', 'PO', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`. **isPort(str)** | check if the string is a valid port number. diff --git a/src/lib/isNumeric.js b/src/lib/isNumeric.js index d0569e689..e1fd01041 100644 --- a/src/lib/isNumeric.js +++ b/src/lib/isNumeric.js @@ -11,9 +11,15 @@ export default function isNumeric(str, options) { } const decimal_char = (options || {}).locale ? decimal[options.locale] : '.'; - if (options && options.thousand_separator) { - return new RegExp(`^[+-]?[0-9]{1,3}(${options.thousand_separator}[0-9]{3})*([${decimal_char}][0-9]+)?$`).test(str); + if (options && options.thousands_separator) { + const separator = `${options.thousands_separator || ''}`; + if (separator.length > 1 || numericNoSymbols.test(separator)) { + throw new TypeError(`Expected non-numeric single character. Received thousand_separator: ${separator}`); + } else { + return new RegExp(`^[+-]?[0-9]{1,3}(${separator}[0-9]{3})*([${decimal_char}][0-9]+)?$`).test(str); + } } return (new RegExp(`^[+-]?([0-9]*[${decimal_char}])?[0-9]+$`)).test(str); } + diff --git a/test/validators.js b/test/validators.js index 354219540..eb8189b53 100644 --- a/test/validators.js +++ b/test/validators.js @@ -2020,7 +2020,7 @@ describe('Validators', () => { test({ validator: 'isNumeric', args: [{ - thousand_separator: '_', + thousands_separator: '_', }], valid: [ '123', @@ -2051,7 +2051,7 @@ describe('Validators', () => { test({ validator: 'isNumeric', args: [{ - thousand_separator: '_', + thousands_separator: '_', locale: 'fr-FR', }], valid: [ @@ -2078,6 +2078,39 @@ describe('Validators', () => { }); }); + it('should not validate numeric strings with invalid thousand_seperator chars', () => { + test({ + validator: 'isNumeric', + args: [{ + thousands_separator: '__', + }], + error: [ + '123', + '123__123', + ], + }); + test({ + validator: 'isNumeric', + args: [{ + thousands_separator: '9', + }], + error: [ + '123', + '1239123', + ], + }); + test({ + validator: 'isNumeric', + args: [{ + thousands_separator: '0', + }], + error: [ + '123', + '1230123', + ], + }); + }); + it('should validate ports', () => { test({ validator: 'isPort',