From b89ebf17e255371d036eb4401bdbb06ca910e813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Zorro?= Date: Mon, 11 Nov 2019 17:22:46 -0500 Subject: [PATCH 1/5] Adding es-CO locale for mobile phones --- lib/isMobilePhone.js | 1 + src/lib/isMobilePhone.js | 1 + test/validators.js | 30 ++++++++++++++++++++++++++++++ validator.js | 1 + 4 files changed, 33 insertions(+) diff --git a/lib/isMobilePhone.js b/lib/isMobilePhone.js index 8e9995fb8..ede3f6637 100644 --- a/lib/isMobilePhone.js +++ b/lib/isMobilePhone.js @@ -53,6 +53,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}$/, + 'es-CO': /^(\+?57)?([1-8]{1}|3[0-9]{2})?[2-9]{1}\d{6}$/, 'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, 'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/, diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index 649cd0fb1..fa3c49476 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -47,6 +47,7 @@ const phones = { 'en-ZA': /^(\+?27|0)\d{9}$/, 'en-ZM': /^(\+?26)?09[567]\d{7}$/, 'en-ZW': /^(\+263)[0-9]{9}$/, + 'es-CO': /^(\+?57)?([1-8]{1}|3[0-9]{2})?[2-9]{1}\d{6}$/, '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}$/, diff --git a/test/validators.js b/test/validators.js index 4219bd047..097173821 100755 --- a/test/validators.js +++ b/test/validators.js @@ -5810,6 +5810,36 @@ describe('Validators', () => { '0533803765', ], }, + { + locale: 'es-CO', + valid: [ + '+573003321235', + '573003321235', + '579871235', + '3003321235', + '3213321235', + '3103321235', + '3253321235', + '3321235', + '574321235', + '5784321235', + '5784321235', + '9821235', + ], + invalid: [ + '1234', + '+57443875615', + '57309875615', + '57109834567', + '5792434567', + '5702345689', + '5714003425432', + '5703013347567', + '069834567', + '0698345', + '969834567', + ], + }, { locale: 'es-CL', valid: [ diff --git a/validator.js b/validator.js index 6cf771ee6..59338b507 100644 --- a/validator.js +++ b/validator.js @@ -2176,6 +2176,7 @@ var phones = { 'en-ZA': /^(\+?27|0)\d{9}$/, 'en-ZM': /^(\+?26)?09[567]\d{7}$/, 'en-ZW': /^(\+263)[0-9]{9}$/, + 'es-CO': /^(\+?57)?([1-8]{1}|3[0-9]{2})?[2-9]{1}\d{6}$/, '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}$/, From eb594162f29b42b4447bc4808422a612fad43629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Zorro?= Date: Fri, 15 Nov 2019 08:32:57 -0500 Subject: [PATCH 2/5] Removing unrelated file changes --- index.js | 2 +- lib/isIP.js | 24 +----------------------- lib/isMobilePhone.js | 1 - 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 2eb99844a..b2af5bb07 100644 --- a/index.js +++ b/index.js @@ -181,7 +181,7 @@ var _isSlug = _interopRequireDefault(require("./lib/isSlug")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } -function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } diff --git a/lib/isIP.js b/lib/isIP.js index b5a924cfd..83dfda2a3 100644 --- a/lib/isIP.js +++ b/lib/isIP.js @@ -58,29 +58,7 @@ function isIP(str) { }); return parts[3] <= 255; } else if (version === '6') { - var addressAndZone = [str]; // ipv6 addresses could have scoped architecture - // according to https://tools.ietf.org/html/rfc4007#section-11 - - if (str.includes('%')) { - addressAndZone = str.split('%'); - - if (addressAndZone.length !== 2) { - // it must be just two parts - return false; - } - - if (!addressAndZone[0].includes(':')) { - // the first part must be the address - return false; - } - - if (addressAndZone[1] === '') { - // the second part must not be empty - return false; - } - } - - var blocks = addressAndZone[0].split(':'); + var blocks = str.split(':'); var foundOmissionBlock = false; // marker to indicate :: // At least some OS accept the last 32 bits of an IPv6 address // (i.e. 2 of the blocks) in IPv4 notation, and RFC 3493 says diff --git a/lib/isMobilePhone.js b/lib/isMobilePhone.js index ede3f6637..cbb133256 100644 --- a/lib/isMobilePhone.js +++ b/lib/isMobilePhone.js @@ -55,7 +55,6 @@ var phones = { 'en-ZM': /^(\+?26)?09[567]\d{7}$/, 'es-CO': /^(\+?57)?([1-8]{1}|3[0-9]{2})?[2-9]{1}\d{6}$/, 'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, - 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, 'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/, 'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, 'es-PA': /^(\+?507)\d{7,8}$/, From 1b1f6c074bd17d65380ca958fc5931d1b0364e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Zorro?= Date: Sat, 6 Jun 2020 06:59:09 -0500 Subject: [PATCH 3/5] Unrelated files --- index.js | 2 +- lib/isIP.js | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b2af5bb07..2eb99844a 100644 --- a/index.js +++ b/index.js @@ -181,7 +181,7 @@ var _isSlug = _interopRequireDefault(require("./lib/isSlug")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } -function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } diff --git a/lib/isIP.js b/lib/isIP.js index 83dfda2a3..b5a924cfd 100644 --- a/lib/isIP.js +++ b/lib/isIP.js @@ -58,7 +58,29 @@ function isIP(str) { }); return parts[3] <= 255; } else if (version === '6') { - var blocks = str.split(':'); + var addressAndZone = [str]; // ipv6 addresses could have scoped architecture + // according to https://tools.ietf.org/html/rfc4007#section-11 + + if (str.includes('%')) { + addressAndZone = str.split('%'); + + if (addressAndZone.length !== 2) { + // it must be just two parts + return false; + } + + if (!addressAndZone[0].includes(':')) { + // the first part must be the address + return false; + } + + if (addressAndZone[1] === '') { + // the second part must not be empty + return false; + } + } + + var blocks = addressAndZone[0].split(':'); var foundOmissionBlock = false; // marker to indicate :: // At least some OS accept the last 32 bits of an IPv6 address // (i.e. 2 of the blocks) in IPv4 notation, and RFC 3493 says From 45f219832f7e74ce2ff40f5502d3802ff9f2307f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Zorro?= Date: Sat, 6 Jun 2020 07:00:46 -0500 Subject: [PATCH 4/5] Adding back es-EC --- lib/isMobilePhone.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/isMobilePhone.js b/lib/isMobilePhone.js index cbb133256..ede3f6637 100644 --- a/lib/isMobilePhone.js +++ b/lib/isMobilePhone.js @@ -55,6 +55,7 @@ var phones = { 'en-ZM': /^(\+?26)?09[567]\d{7}$/, 'es-CO': /^(\+?57)?([1-8]{1}|3[0-9]{2})?[2-9]{1}\d{6}$/, 'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/, + 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, 'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/, 'es-MX': /^(\+?52)?(1|01)?\d{10,11}$/, 'es-PA': /^(\+?507)\d{7,8}$/, From c169f2921795162567c8901baf33090eeeb7b31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Zorro?= Date: Sun, 7 Jun 2020 15:14:32 -0500 Subject: [PATCH 5/5] Adding es-CO to isMobilePhone README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d018f19a..6dad0afa4 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ Validator | Description **isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). **isMD5(str)** | check if the string is a MD5 hash.

Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA). **isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format -**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', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', '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-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW' , 'es-CL', 'es-CR', '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', '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', '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`. +**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', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', '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-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW' , 'es-CL', 'es-CO', 'es-CR', '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', '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', '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']`.