From 4e8827b39474db8db26e9fe23b56592be4d7d102 Mon Sep 17 00:00:00 2001 From: Sarah Ryan Date: Thu, 26 May 2016 17:36:24 -0700 Subject: [PATCH 1/4] Allow prepended zeroes in toInt() --- src/lib/isInt.js | 2 +- test/validators.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/isInt.js b/src/lib/isInt.js index 7d4204410..d49f55ea2 100644 --- a/src/lib/isInt.js +++ b/src/lib/isInt.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/; +const int = /^[-+]?[0-9]+$/; export default function isInt(str, options) { assertString(str); diff --git a/test/validators.js b/test/validators.js index d933ca146..2a68015e3 100644 --- a/test/validators.js +++ b/test/validators.js @@ -890,11 +890,11 @@ describe('Validators', function () { '123', '-0', '+1', - ], - invalid: [ '01', '-01', '000', + ], + invalid: [ '100e10', '123.123', ' ', From f624d4641198af87e5d5bba12eca0f2bdac6a8f4 Mon Sep 17 00:00:00 2001 From: Sarah Ryan Date: Fri, 27 May 2016 10:44:06 -0700 Subject: [PATCH 2/4] Disable `no-control-regex` eslint checks where necessary. --- src/lib/isAscii.js | 2 ++ src/lib/isEmail.js | 2 ++ src/lib/isMultibyte.js | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/lib/isAscii.js b/src/lib/isAscii.js index 538f1368b..6bce344de 100644 --- a/src/lib/isAscii.js +++ b/src/lib/isAscii.js @@ -1,6 +1,8 @@ import assertString from './util/assertString'; +/* eslint-disable no-control-regex */ const ascii = /^[\x00-\x7F]+$/; +/* eslint-enable no-control-regex */ export default function isAscii(str) { assertString(str); diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index d371a5e56..58e76f2a7 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -11,12 +11,14 @@ const default_email_options = { }; /* eslint-disable max-len */ +/* eslint-disable no-control-regex */ const displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i; const emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i; const quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i; const emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i; const quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i; /* eslint-enable max-len */ +/* eslint-enable no-control-regex */ export default function isEmail(str, options) { assertString(str); diff --git a/src/lib/isMultibyte.js b/src/lib/isMultibyte.js index 332196f53..5354cdfda 100644 --- a/src/lib/isMultibyte.js +++ b/src/lib/isMultibyte.js @@ -1,6 +1,8 @@ import assertString from './util/assertString'; +/* eslint-disable no-control-regex */ const multibyte = /[^\x00-\x7F]/; +/* eslint-enable no-control-regex */ export default function isMultibyte(str) { assertString(str); From a3e3880203ec17f65488dfdc500db3d85d5d0975 Mon Sep 17 00:00:00 2001 From: Sarah Ryan Date: Fri, 27 May 2016 13:53:11 -0700 Subject: [PATCH 3/4] Add `allow_leading_zeroes` option to isInt() --- src/lib/isInt.js | 19 ++++++++++++++++--- test/validators.js | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/lib/isInt.js b/src/lib/isInt.js index d49f55ea2..01fbd1c97 100644 --- a/src/lib/isInt.js +++ b/src/lib/isInt.js @@ -1,10 +1,23 @@ import assertString from './util/assertString'; -const int = /^[-+]?[0-9]+$/; +const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/; +const intLeadingZeroes = /^[-+]?[0-9]+$/; export default function isInt(str, options) { assertString(str); options = options || {}; - return int.test(str) && (!options.hasOwnProperty('min') || - str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max); + + // Get the regex to use for testing, based on whether + // leading zeroes are allowed or not. + let regex = ( + options.hasOwnProperty('allow_leading_zeroes') && options.allow_leading_zeroes ? + intLeadingZeroes : + int + ); + + // Check min/max + let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min); + let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max); + + return regex.test(str) && minCheckPassed && maxCheckPassed; } diff --git a/test/validators.js b/test/validators.js index 2a68015e3..7ea387772 100644 --- a/test/validators.js +++ b/test/validators.js @@ -883,6 +883,27 @@ describe('Validators', function () { it('should validate integers', function () { test({ validator: 'isInt', + valid: [ + '13', + '123', + '0', + '123', + '-0', + '+1', + ], + invalid: [ + '01', + '-01', + '000', + '100e10', + '123.123', + ' ', + '', + ], + }); + test({ + validator: 'isInt', + args: [{ allow_leading_zeroes: true }], valid: [ '13', '123', @@ -893,6 +914,8 @@ describe('Validators', function () { '01', '-01', '000', + '-000', + '+000', ], invalid: [ '100e10', From 4923be5f650ecd94907259425d468c869dd77122 Mon Sep 17 00:00:00 2001 From: Sarah Ryan Date: Fri, 27 May 2016 18:32:16 -0700 Subject: [PATCH 4/4] Update README describing the `allow_leading_zeroes` option for isInt() --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91c52cf9d..a6c2ad937 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Passing anything other than a string is an error. - **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier). - **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date. - **isIn(str, values)** - check if the string is in a array of allowed values. -- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). +- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to true will accept integer values with leading zeroes (e.g. `{ allow_leading_zeroes: true }`). - **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse). - **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. - **isLowercase(str)** - check if the string is lowercase.