From c63b44fc0391f4280cc8fb443aff38c2bf5ce846 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Tue, 26 May 2020 00:44:29 +0530 Subject: [PATCH 1/9] Fixes #1303 --- src/lib/isMobilePhone.js | 2 +- test/validators.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index a1cea9971..a194ba26d 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -90,7 +90,7 @@ const phones = { 'tr-TR': /^(\+?90|0)?5\d{9}$/, 'uk-UA': /^(\+?38|8)?0\d{9}$/, 'vi-VN': /^(\+?84|0)((3([2-9]))|(5([2689]))|(7([0|6-9]))|(8([1-6|89]))|(9([0-9])))([0-9]{7})$/, - 'zh-CN': /^((\+|00)86)?1([358][0-9]|4[579]|6[67]|7[01235678]|9[189])[0-9]{8}$/, + 'zh-CN': /^((\+|00)86)?1([3568][0-9]|4[579]|6[67]|7[01235678]|9[189])[0-9]{8}$/, 'zh-TW': /^(\+?886\-?|0)?9\d{8}$/, }; /* eslint-enable max-len */ diff --git a/test/validators.js b/test/validators.js index ddceedead..c0026cd42 100755 --- a/test/validators.js +++ b/test/validators.js @@ -4954,6 +4954,7 @@ describe('Validators', () => { '+8619812341234', '+8619112341234', '17269427292', + '16565600001', '+8617269427292', '008617269427292', ], From c8419de4658b0dc431cc0ac527b52ce22f401459 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 13:18:19 +0530 Subject: [PATCH 2/9] Added postal code for nepal --- es/lib/isPostalCode.js | 3 ++- lib/isPostalCode.js | 5 +++-- src/lib/isPostalCode.js | 1 + test/validators.js | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/es/lib/isPostalCode.js b/es/lib/isPostalCode.js index a33300724..0e0cf7ec0 100644 --- a/es/lib/isPostalCode.js +++ b/es/lib/isPostalCode.js @@ -41,6 +41,7 @@ var patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, @@ -59,7 +60,7 @@ var patterns = { ZM: fiveDigit }; export var locales = Object.keys(patterns); -export default function (str, locale) { +export default function isPostalCode(str, locale) { assertString(str); if (locale in patterns) { diff --git a/lib/isPostalCode.js b/lib/isPostalCode.js index 7dab73f7b..333152fba 100644 --- a/lib/isPostalCode.js +++ b/lib/isPostalCode.js @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = _default; +exports.default = isPostalCode; exports.locales = void 0; var _assertString = _interopRequireDefault(require("./util/assertString")); @@ -52,6 +52,7 @@ var patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, @@ -72,7 +73,7 @@ var patterns = { var locales = Object.keys(patterns); exports.locales = locales; -function _default(str, locale) { +function isPostalCode(str, locale) { (0, _assertString.default)(str); if (locale in patterns) { diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index 4800b3cea..fdb537de4 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -43,6 +43,7 @@ const patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, diff --git a/test/validators.js b/test/validators.js index 6dd77f8f0..22b2927cd 100755 --- a/test/validators.js +++ b/test/validators.js @@ -7962,6 +7962,22 @@ describe('Validators', () => { '3997 GH', ], }, + { + locale: 'NP', + valid: [ + '10811', + '32600', + '56806', + '977', + ], + invalid: [ + '11977', + 'asds', + '13 32', + '-977', + '97765', + ], + }, { locale: 'PL', valid: [ From f513c292912b60261984bd96c131c1231ad8977c Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 14:02:17 +0530 Subject: [PATCH 3/9] Added NP locale to postal code --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f930719a8..514c4c0d3 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Validator | Description **isOctal(str)** | check if the string is a valid octal number. **isPassportNumber(str, countryCode)** | check if the string is a valid passport number relative to a specific country code. **isPort(str)** | check if the string is a valid port number. -**isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). +**isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. **isURL(str [, options])** | check if the string is an URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed. From 4d7828d420f46193028609ae9eaa16042c7f2dad Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Wed, 27 May 2020 14:34:41 +0530 Subject: [PATCH 4/9] Added country code to passport READNE --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 514c4c0d3..ff939a635 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Validator | Description **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}`. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`). **isOctal(str)** | check if the string is a valid octal number. -**isPassportNumber(str, countryCode)** | check if the string is a valid passport number relative to a specific country code. +**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', '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', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`. **isPort(str)** | check if the string is a valid port number. **isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). From 778dfd71ac4a302247149a6c6a0b3aecfdecdb4f Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Thu, 28 May 2020 07:02:31 +0530 Subject: [PATCH 5/9] Added norway identity card --- README.md | 2 +- es/lib/isIdentityCard.js | 20 ++++++- lib/isIdentityCard.js | 20 ++++++- src/lib/isIdentityCard.js | 21 ++++++- test/validators.js | 18 ++++++ validator.js | 115 ++++++++++++++++++++++++++++---------- 6 files changed, 162 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index e3fc8eaa7..97fa140f5 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Validator | Description **isHexColor(str)** | check if the string is a hexadecimal color. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). **isRgbColor(str, [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. -**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['ES', 'IN', 'zh-TW', 'he-IL', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. +**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['ES', 'IN', 'NO', 'zh-TW', 'he-IL', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. **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 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4). **isIP(str [, version])** | check if the string is an IP (version 4 or 6). diff --git a/es/lib/isIdentityCard.js b/es/lib/isIdentityCard.js index b845a397b..de8714aeb 100644 --- a/es/lib/isIdentityCard.js +++ b/es/lib/isIdentityCard.js @@ -29,8 +29,7 @@ var validators = { var p = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]]; // sanitize user input - var sanitized = str.trim(); - console.log(sanitized); // validate the data structure + var sanitized = str.trim(); // validate the data structure if (!DNI.test(sanitized)) { return false; @@ -43,6 +42,23 @@ var validators = { }); return c === 0; }, + NO: function NO(str) { + var sanitized = str.trim(); + if (isNaN(Number(sanitized))) return false; + if (sanitized.length !== 11) return false; + if (sanitized === '00000000000') return false; // https://no.wikipedia.org/wiki/F%C3%B8dselsnummer + + var f = sanitized.split('').map(Number); + var k1 = (11 - (3 * f[0] + 7 * f[1] + 6 * f[2] + 1 * f[3] + 8 * f[4] + 9 * f[5] + 4 * f[6] + 5 * f[7] + 2 * f[8]) % 11) % 11; + var k2 = (11 - (5 * f[0] + 4 * f[1] + 3 * f[2] + 2 * f[3] + 7 * f[4] + 6 * f[5] + 5 * f[6] + 4 * f[7] + 3 * f[8] + 2 * k1) % 11) % 11; + + if (k1 === 11) { + k1 = 0; + } + + if (k1 !== f[9] || k2 !== f[10]) return false; + return true; + }, 'he-IL': function heIL(str) { var DNI = /^\d{9}$/; // sanitize user input diff --git a/lib/isIdentityCard.js b/lib/isIdentityCard.js index 8c8575952..7f5c63d5d 100644 --- a/lib/isIdentityCard.js +++ b/lib/isIdentityCard.js @@ -39,8 +39,7 @@ var validators = { var p = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]]; // sanitize user input - var sanitized = str.trim(); - console.log(sanitized); // validate the data structure + var sanitized = str.trim(); // validate the data structure if (!DNI.test(sanitized)) { return false; @@ -53,6 +52,23 @@ var validators = { }); return c === 0; }, + NO: function NO(str) { + var sanitized = str.trim(); + if (isNaN(Number(sanitized))) return false; + if (sanitized.length !== 11) return false; + if (sanitized === '00000000000') return false; // https://no.wikipedia.org/wiki/F%C3%B8dselsnummer + + var f = sanitized.split('').map(Number); + var k1 = (11 - (3 * f[0] + 7 * f[1] + 6 * f[2] + 1 * f[3] + 8 * f[4] + 9 * f[5] + 4 * f[6] + 5 * f[7] + 2 * f[8]) % 11) % 11; + var k2 = (11 - (5 * f[0] + 4 * f[1] + 3 * f[2] + 2 * f[3] + 7 * f[4] + 6 * f[5] + 5 * f[6] + 4 * f[7] + 3 * f[8] + 2 * k1) % 11) % 11; + + if (k1 === 11) { + k1 = 0; + } + + if (k1 !== f[9] || k2 !== f[10]) return false; + return true; + }, 'he-IL': function heIL(str) { var DNI = /^\d{9}$/; // sanitize user input diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 65416f59f..11c72cf77 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -61,7 +61,6 @@ const validators = { // sanitize user input const sanitized = str.trim(); - console.log(sanitized); // validate the data structure if (!DNI.test(sanitized)) { @@ -76,6 +75,26 @@ const validators = { return c === 0; }, + NO: (str) => { + const sanitized = str.trim(); + if (isNaN(Number(sanitized))) return false; + if (sanitized.length !== 11) return false; + if (sanitized === '00000000000') return false; + + // https://no.wikipedia.org/wiki/F%C3%B8dselsnummer + const f = sanitized.split('').map(Number); + let k1 = (11 - (((3 * f[0]) + (7 * f[1]) + (6 * f[2]) + + (1 * f[3]) + (8 * f[4]) + (9 * f[5]) + (4 * f[6]) + + (5 * f[7]) + (2 * f[8])) % 11)) % 11; + let k2 = (11 - (((5 * f[0]) + (4 * f[1]) + (3 * f[2]) + + (2 * f[3]) + (7 * f[4]) + (6 * f[5]) + (5 * f[6]) + + (4 * f[7]) + (3 * f[8]) + (2 * k1)) % 11)) % 11; + if (k1 === 11) { + k1 = 0; + } + if (k1 !== f[9] || k2 !== f[10]) return false; + return true; + }, 'he-IL': (str) => { const DNI = /^\d{9}$/; diff --git a/test/validators.js b/test/validators.js index c6031db10..60fcf1b8d 100755 --- a/test/validators.js +++ b/test/validators.js @@ -3978,6 +3978,24 @@ describe('Validators', () => { 'X1234567L', ], }, + { + locale: 'NO', + valid: [ + '09053426694', + '26028338723', + '08031470790', + '12051539514', + '02077448074', + '14035638319', + '13031379673', + '29126214926', + ], + invalid: [ + '09053426699', + '26028338724', + '92031470790', + ], + }, { locale: 'he-IL', valid: [ diff --git a/validator.js b/validator.js index fe8765116..2018aa700 100644 --- a/validator.js +++ b/validator.js @@ -82,7 +82,7 @@ function _unsupportedIterableToArray(o, minLen) { if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; - if (n === "Map" || n === "Set") return Array.from(n); + if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } @@ -1023,6 +1023,7 @@ function isNumeric(str, options) { * https://docs.microsoft.com/en-us/microsoft-365/compliance/eu-passport-number -- EU Passport Number * https://countrycode.org/ -- Country Codes */ + var passportRegexByCountryCode = { AM: /^[A-Z]{2}\d{7}$/, // ARMENIA @@ -1070,6 +1071,8 @@ var passportRegexByCountryCode = { // HUNGARY IE: /^[A-Z0-9]{2}\d{7}$/, // IRELAND + IN: /^[A-Z]{1}-?\d{7}$/, + // INDIA IS: /^(A)\d{7}$/, // ICELAND IT: /^[A-Z0-9]{2}\d{7}$/, @@ -1117,7 +1120,9 @@ var passportRegexByCountryCode = { */ function isPassportNumber(str, countryCode) { + assertString(str); /** Remove All Whitespaces, Convert to UPPERCASE */ + var normalizedStr = str.replace(/\s/g, '').toUpperCase(); return countryCode.toUpperCase() in passportRegexByCountryCode && passportRegexByCountryCode[countryCode].test(normalizedStr); } @@ -1345,6 +1350,7 @@ var ibanRegexThroughCountryCode = { IE: /^(IE[0-9]{2})[A-Z0-9]{4}\d{14}$/, IL: /^(IL[0-9]{2})\d{19}$/, IQ: /^(IQ[0-9]{2})[A-Z]{4}\d{15}$/, + IR: /^(IR[0-9]{2})0\d{2}0\d{18}$/, IS: /^(IS[0-9]{2})\d{22}$/, IT: /^(IT[0-9]{2})[A-Z]{1}\d{10}[A-Z0-9]{12}$/, JO: /^(JO[0-9]{2})[A-Z]{4}\d{22}$/, @@ -1356,7 +1362,6 @@ var ibanRegexThroughCountryCode = { LT: /^(LT[0-9]{2})\d{16}$/, LU: /^(LU[0-9]{2})\d{3}[A-Z0-9]{13}$/, LV: /^(LV[0-9]{2})[A-Z]{4}[A-Z0-9]{13}$/, - LY: /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, MC: /^(MC[0-9]{2})\d{10}[A-Z0-9]{11}\d{2}$/, MD: /^(MD[0-9]{2})[A-Z0-9]{20}$/, ME: /^(ME[0-9]{2})\d{18}$/, @@ -1471,10 +1476,42 @@ function isHash(str, algorithm) { return hash.test(str); } -var jwt = /^([A-Za-z0-9\-_~+\/]+[=]{0,2})\.([A-Za-z0-9\-_~+\/]+[=]{0,2})(?:\.([A-Za-z0-9\-_~+\/]+[=]{0,2}))?$/; +var notBase64 = /[^A-Z0-9+\/=]/i; +var urlSafeBase64 = /^[A-Z0-9_\-]+$/i; +var defaultBase64Options = { + urlSafe: false +}; +function isBase64(str, options) { + assertString(str); + options = merge(options, defaultBase64Options); + var len = str.length; + + if (options.urlSafe) { + return urlSafeBase64.test(str); + } + + if (!len || len % 4 !== 0 || notBase64.test(str)) { + return false; + } + + var firstPaddingChar = str.indexOf('='); + return firstPaddingChar === -1 || firstPaddingChar === len - 1 || firstPaddingChar === len - 2 && str[len - 1] === '='; +} + function isJWT(str) { assertString(str); - return jwt.test(str); + var dotSplit = str.split('.'); + var len = dotSplit.length; + + if (len > 3 || len < 2) { + return false; + } + + return dotSplit.reduce(function (acc, currElem) { + return acc && isBase64(currElem, { + urlSafe: true + }); + }, true); } function isJSON(str) { @@ -1642,6 +1679,43 @@ var validators = { }); return sanitized.endsWith(controlDigits[number % 23]); }, + IN: function IN(str) { + var DNI = /^[1-9]\d{3}\s?\d{4}\s?\d{4}$/; // multiplication table + + var d = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 0, 6, 7, 8, 9, 5], [2, 3, 4, 0, 1, 7, 8, 9, 5, 6], [3, 4, 0, 1, 2, 8, 9, 5, 6, 7], [4, 0, 1, 2, 3, 9, 5, 6, 7, 8], [5, 9, 8, 7, 6, 0, 4, 3, 2, 1], [6, 5, 9, 8, 7, 1, 0, 4, 3, 2], [7, 6, 5, 9, 8, 2, 1, 0, 4, 3], [8, 7, 6, 5, 9, 3, 2, 1, 0, 4], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]]; // permutation table + + var p = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]]; // sanitize user input + + var sanitized = str.trim(); // validate the data structure + + if (!DNI.test(sanitized)) { + return false; + } + + var c = 0; + var invertedArray = sanitized.replace(/\s/g, '').split('').map(Number).reverse(); + invertedArray.forEach(function (val, i) { + c = d[c][p[i % 8][val]]; + }); + return c === 0; + }, + NO: function NO(str) { + var sanitized = str.trim(); + if (isNaN(Number(sanitized))) return false; + if (sanitized.length !== 11) return false; + if (sanitized === '00000000000') return false; // https://no.wikipedia.org/wiki/F%C3%B8dselsnummer + + var f = sanitized.split('').map(Number); + var k1 = (11 - (3 * f[0] + 7 * f[1] + 6 * f[2] + 1 * f[3] + 8 * f[4] + 9 * f[5] + 4 * f[6] + 5 * f[7] + 2 * f[8]) % 11) % 11; + var k2 = (11 - (5 * f[0] + 4 * f[1] + 3 * f[2] + 2 * f[3] + 7 * f[4] + 6 * f[5] + 5 * f[6] + 4 * f[7] + 3 * f[8] + 2 * k1) % 11) % 11; + + if (k1 === 11) { + k1 = 0; + } + + if (k1 !== f[9] || k2 !== f[10]) return false; + return true; + }, 'he-IL': function heIL(str) { var DNI = /^\d{9}$/; // sanitize user input @@ -2054,6 +2128,7 @@ var phones = { 'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/, 'ar-JO': /^(\+?962|0)?7[789]\d{7}$/, 'ar-KW': /^(\+?965)[569]\d{7}$/, + 'ar-LY': /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, 'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/, 'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/, 'ar-TN': /^(\+?216)?[2459]\d{7}$/, @@ -2064,6 +2139,7 @@ var phones = { 'da-DK': /^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$/, 'de-DE': /^(\+49)?0?1(5[0-25-9]\d|6([23]|0\d?)|7([0-57-9]|6\d))\d{7}$/, 'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/, + 'de-CH': /^(\+41|0)(7[5-9])\d{1,7}$/, 'el-GR': /^(\+?30|0)?(69\d{8})$/, 'en-AU': /^(\+?61|0)4\d{8}$/, 'en-GB': /^(\+?44|0)7\d{9}$/, @@ -2087,6 +2163,7 @@ var phones = { 'en-US': /^((\+1|1)?( |-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})( |-)?([2-9][0-9]{2}( |-)?[0-9]{4})$/, 'en-ZA': /^(\+?27|0)\d{9}$/, 'en-ZM': /^(\+?26)?09[567]\d{7}$/, + 'en-ZW': /^(\+263)[0-9]{9}$/, 'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, 'es-CR': /^(\+506)?[2-8]\d{7}$/, 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, @@ -2133,7 +2210,7 @@ var phones = { 'tr-TR': /^(\+?90|0)?5\d{9}$/, 'uk-UA': /^(\+?38|8)?0\d{9}$/, 'vi-VN': /^(\+?84|0)((3([2-9]))|(5([2689]))|(7([0|6-9]))|(8([1-6|89]))|(9([0-9])))([0-9]{7})$/, - 'zh-CN': /^((\+|00)86)?1([358][0-9]|4[579]|6[67]|7[01235678]|9[189])[0-9]{8}$/, + 'zh-CN': /^((\+|00)86)?1([3568][0-9]|4[579]|6[67]|7[01235678]|9[189])[0-9]{8}$/, 'zh-TW': /^(\+?886\-?|0)?9\d{8}$/ }; /* eslint-enable max-len */ @@ -2361,25 +2438,6 @@ function isBase32(str) { return false; } -var notBase64 = /[^A-Z0-9+\/=]/i; -function isBase64(str) { - var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - assertString(str); - - if (options.urlSafe) { - return /^[A-Za-z0-9_-]+$/.test(str); - } - - var len = str.length; - - if (!len || len % 4 !== 0 || notBase64.test(str)) { - return false; - } - - var firstPaddingChar = str.indexOf('='); - return firstPaddingChar === -1 || firstPaddingChar === len - 1 || firstPaddingChar === len - 2 && str[len - 1] === '='; -} - var validMediaType = /^[a-z]+\/[a-z0-9\-\+]+$/i; var validAttribute = /^[a-z\-]+=[a-z0-9\-]+$/i; var validData = /^[a-z0-9!\$&'\(\)\*\+,;=\-\._~:@\/\?%\s]*$/i; @@ -2467,13 +2525,13 @@ function isMimeType(str) { var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/; var _long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/; -var isLatLong = function (str) { +function isLatLong(str) { assertString(str); if (!str.includes(',')) return false; var pair = str.split(','); if (pair[0].startsWith('(') && !pair[1].endsWith(')') || pair[1].endsWith(')') && !pair[0].startsWith('(')) return false; return lat.test(pair[0]) && _long.test(pair[1]); -}; +} var threeDigit = /^\d{3}$/; var fourDigit = /^\d{4}$/; @@ -2516,6 +2574,7 @@ var patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, @@ -2534,7 +2593,7 @@ var patterns = { ZM: fiveDigit }; var locales$4 = Object.keys(patterns); -var isPostalCode = function (str, locale) { +function isPostalCode(str, locale) { assertString(str); if (locale in patterns) { @@ -2556,7 +2615,7 @@ var isPostalCode = function (str, locale) { } throw new Error("Invalid locale '".concat(locale, "'")); -}; +} function ltrim(str, chars) { assertString(str); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping From 3418d288ad11ecea297ec47f7d7d9005e0c4640d Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Fri, 29 May 2020 00:04:49 +0530 Subject: [PATCH 6/9] Fix order in readme --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6e41a662b..df6c9d175 100644 --- a/README.md +++ b/README.md @@ -90,20 +90,20 @@ Validator | Description **isBase32(str)** | check if a string is base32 encoded. **isBase64(str, [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) **isBefore(str [, date])** | check if the string is a date that's before the specified date. -**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number). **isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code. **isBoolean(str)** | check if a string is a boolean. +**isBtcAddress(str)** | check if the string is a valid BTC address. **isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. **isCreditCard(str)** | check if the string is a credit card. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. -**isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums. -**isBtcAddress(str)** | check if the string is a valid BTC address. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). **isDate(input, [, format])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`format` is a string and defaults to `YYYY/MM/DD` **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`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', 'ku-IQ', 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']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. +**isEAN(str)** | check if the string is an EAN (European Article Number). **isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. **isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace:false }`. +**isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums. **isFloat(str [, options])** | check if the string is a float.

`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.

`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.

`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']`. Locale list is `validator.isFloatLocales`. **isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`. **isFullWidth(str)** | check if the string contains any full-width chars. @@ -112,21 +112,19 @@ Validator | Description **isHexadecimal(str)** | check if the string is a hexadecimal number. **isHexColor(str)** | check if the string is a hexadecimal color. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). -**isRgbColor(str, [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. +**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number). **isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['ES', 'IN', 'NO', 'zh-TW', 'he-IL', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. **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 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4). **isIP(str [, version])** | check if the string is an IP (version 4 or 6). **isIPRange(str)** | check if the string is an IP Range(version 4 only). **isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13). -**isEAN(str)** | check if the string is an EAN (European Article Number). **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; for additional checks for valid dates, e.g. invalidates dates like `2009-02-29`, pass `options` object as a second parameter with `options.strict = true`. **isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. **isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code. -**isISO8601(str)** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date; for additional checks for valid dates, e.g. invalidates dates like `2009-02-29`, pass `options` object as a second parameter with `options.strict = true`. -**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code). -**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date. +**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isJSON(str)** | check if the string is valid JSON (note: uses JSON.parse). **isJWT(str)** | check if the string is valid JWT token. **isLatLong(str)**                     | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`. @@ -145,11 +143,13 @@ Validator | Description **isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', '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', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`. **isPort(str)** | check if the string is a valid port number. **isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). +**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date. +**isRgbColor(str, [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. +**isUppercase(str)** | check if the string is uppercase. **isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`] **isURL(str [, options])** | check if the string is an URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed. -**isUppercase(str)** | check if the string is uppercase. **isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5). **isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars. **isWhitelisted(str, chars)** | checks characters if they appear in the whitelist. @@ -163,7 +163,6 @@ Sanitizer | Description -------------------------------------- | ------------------------------- **blacklist(input, chars)** | remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `blacklist(input, '\\[\\]')`. **escape(input)** | replace `<`, `>`, `&`, `'`, `"` and `/` with HTML entities. -**unescape(input)** | replaces HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`. **ltrim(input [, chars])** | trim characters from the left-side of the input. **normalizeEmail(email [, options])** | canonicalizes an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand)

`options` is an object with the following keys and default values:
  • *all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it's case insensitive per RFC 1035.
  • *gmail_lowercase: true* - GMail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, GMail addresses are lowercased regardless of the value of this setting.
  • *gmail_remove_dots: true*: Removes dots from the local part of the email address, as GMail ignores them (e.g. "john.doe" and "johndoe" are considered equal).
  • *gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@gmail.com" becomes "foo@gmail.com").
  • *gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.
  • *outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.
  • *outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@outlook.com" becomes "foo@outlook.com").
  • *yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.
  • *yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "foo-bar@yahoo.com" becomes "foo@yahoo.com").
  • *icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.
  • *icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@icloud.com" becomes "foo@icloud.com").
**rtrim(input [, chars])** | trim characters from the right-side of the input. @@ -173,6 +172,7 @@ Sanitizer | Description **toFloat(input)** | convert the input string to a float, or `NaN` if the input is not a float. **toInt(input [, radix])** | convert the input string to an integer, or `NaN` if the input is not an integer. **trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input. +**unescape(input)** | replaces HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`. **whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`. ### XSS Sanitization From e35e2f0cfd8e20fef611203905af52e22ecebf61 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Sat, 30 May 2020 13:47:33 +0530 Subject: [PATCH 7/9] Added ignoreCase to contains --- es/lib/contains.js | 9 +++++++-- lib/contains.js | 11 +++++++++-- src/lib/contains.js | 12 ++++++++++-- test/validators.js | 9 +++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/es/lib/contains.js b/es/lib/contains.js index 10bd66860..663b96718 100644 --- a/es/lib/contains.js +++ b/es/lib/contains.js @@ -1,6 +1,11 @@ import assertString from './util/assertString'; import toString from './util/toString'; -export default function contains(str, elem) { +import merge from './util/merge'; +var defaulContainsOptions = { + ignoreCase: false +}; +export default function contains(str, elem, options) { assertString(str); - return str.indexOf(toString(elem)) >= 0; + options = merge(options, defaulContainsOptions); + return options.ignoreCase ? str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 : str.indexOf(toString(elem)) >= 0; } \ No newline at end of file diff --git a/lib/contains.js b/lib/contains.js index b02fda2cd..c67e0c726 100644 --- a/lib/contains.js +++ b/lib/contains.js @@ -9,11 +9,18 @@ var _assertString = _interopRequireDefault(require("./util/assertString")); var _toString = _interopRequireDefault(require("./util/toString")); +var _merge = _interopRequireDefault(require("./util/merge")); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -function contains(str, elem) { +var defaulContainsOptions = { + ignoreCase: false +}; + +function contains(str, elem, options) { (0, _assertString.default)(str); - return str.indexOf((0, _toString.default)(elem)) >= 0; + options = (0, _merge.default)(options, defaulContainsOptions); + return options.ignoreCase ? str.toLowerCase().indexOf((0, _toString.default)(elem).toLowerCase()) >= 0 : str.indexOf((0, _toString.default)(elem)) >= 0; } module.exports = exports.default; diff --git a/src/lib/contains.js b/src/lib/contains.js index fd8c578de..ec083fa18 100644 --- a/src/lib/contains.js +++ b/src/lib/contains.js @@ -1,7 +1,15 @@ import assertString from './util/assertString'; import toString from './util/toString'; +import merge from './util/merge'; -export default function contains(str, elem) { +const defaulContainsOptions = { + ignoreCase: false, +}; + +export default function contains(str, elem, options) { assertString(str); - return str.indexOf(toString(elem)) >= 0; + options = merge(options, defaulContainsOptions); + return options.ignoreCase ? + str.toLowerCase().indexOf(toString(elem).toLowerCase()) >= 0 : + str.indexOf(toString(elem)) >= 0; } diff --git a/test/validators.js b/test/validators.js index 60fcf1b8d..374cf88dd 100755 --- a/test/validators.js +++ b/test/validators.js @@ -3496,6 +3496,15 @@ describe('Validators', () => { valid: ['foo', 'foobar', 'bazfoo'], invalid: ['bar', 'fobar'], }); + + test({ + validator: 'contains', + args: ['foo', { + ignoreCase: true, + }], + valid: ['Foo', 'FOObar', 'BAZfoo'], + invalid: ['bar', 'fobar', 'baxoof'], + }); }); it('should validate strings against a pattern', () => { From af08558c7184f655eeb4245af61acf52c6f58a22 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Sat, 30 May 2020 13:54:02 +0530 Subject: [PATCH 8/9] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1833bbc86..d92811cfe 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ Here is a list of the validators currently available. Validator | Description --------------------------------------- | -------------------------------------- -***contains(str, seed)*** | check if the string contains the seed. +**contains(str, seed [, options ])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false}`.
`ignoreCase` specified whether the case of the substring be same or not. **equals(str, comparison)** | check if the string matches the comparison. **isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now). **isAlpha(str [, locale])** | check if the string contains only letters (a-zA-Z).

Locale 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', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. From 752410a39fb39b1392bff30978d642333a00a419 Mon Sep 17 00:00:00 2001 From: Paras Gupta Date: Sat, 30 May 2020 16:14:16 +0530 Subject: [PATCH 9/9] Update readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d92811cfe..774cb214c 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Validator | Description **isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.

Locale 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', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. **isAscii(str)** | check if the string contains ASCII chars only. **isBase32(str)** | check if a string is base32 encoded. -**isBase64(str, [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) +**isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) **isBefore(str [, date])** | check if the string is a date that's before the specified date. **isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code. **isBoolean(str)** | check if a string is a boolean. @@ -97,7 +97,7 @@ Validator | Description **isCreditCard(str)** | check if the string is a credit card. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). -**isDate(input, [, format])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`format` is a string and defaults to `YYYY/MM/DD` +**isDate(input [, format])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`format` is a string and defaults to `YYYY/MM/DD` **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`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', 'ku-IQ', 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']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. **isEAN(str)** | check if the string is an EAN (European Article Number). @@ -127,7 +127,7 @@ Validator | Description **isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).

`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. **isJWT(str)** | check if the string is valid JWT token. -**isLatLong(str)**                     | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`. +**isLatLong(str)** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`. **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. **isLocale(str)** | check if the string is a locale **isLowercase(str)** | check if the string is lowercase. @@ -144,7 +144,7 @@ Validator | Description **isPort(str)** | check if the string is a valid port number. **isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). **isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date. -**isRgbColor(str, [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. +**isRgbColor(str [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. **isUppercase(str)** | check if the string is uppercase. @@ -254,4 +254,4 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [bower]: http://bower.io/ [mongoid]: http://docs.mongodb.org/manual/reference/object-id/ -[ISIN]: https://en.wikipedia.org/wiki/International_Securities_Identification_Number +[ISIN]: https://en.wikipedia.org/wiki/International_Securities_Identification_Number \ No newline at end of file