From 9e73a1c12994b4bf08e21043dce9ac9984cc9b43 Mon Sep 17 00:00:00 2001
From: aidos42 <75936428+aidos42@users.noreply.github.com>
Date: Mon, 27 Mar 2023 12:54:37 +0300
Subject: [PATCH 1/4] feat(isMobilePhone): add locales Wallis and Futuna fr-WF
(#2209)
---
README.md | 2 +-
src/lib/isMobilePhone.js | 1 +
test/validators.test.js | 26 ++++++++++++++++++++++++++
3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index d75f15b27..af22345ef 100644
--- a/README.md
+++ b/README.md
@@ -150,7 +150,7 @@ Validator | Description
**isMagnetURI(str)** | check if the string is a [Magnet URI format][Magnet URI Format].
**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][MIME 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-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', '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`.
+**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-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', '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` determines 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', 'fr-CA', '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']`.
diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js
index a901a94d8..6a8f98790 100644
--- a/src/lib/isMobilePhone.js
+++ b/src/lib/isMobilePhone.js
@@ -104,6 +104,7 @@ const phones = {
'fr-MQ': /^(\+?596|0|00596)[67]\d{8}$/,
'fr-PF': /^(\+?689)?8[789]\d{6}$/,
'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/,
+ 'fr-WF': /^(\+681)?\d{6}$/,
'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/,
'hu-HU': /^(\+?36|06)(20|30|31|50|70)\d{7}$/,
'id-ID': /^(\+?62|0)8(1[123456789]|2[1238]|3[1238]|5[12356789]|7[78]|9[56789]|8[123456789])([\s?|\d]{5,11})$/,
diff --git a/test/validators.test.js b/test/validators.test.js
index 6816a3bbb..b4888457b 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -7764,6 +7764,32 @@ describe('Validators', () => {
'6898912345',
],
},
+ {
+ locale: 'fr-WF',
+ valid: [
+ '+681408500',
+ '+681499387',
+ '+681728590',
+ '+681808542',
+ '+681828540',
+ '+681832014',
+ '408500',
+ '499387',
+ '728590',
+ '808542',
+ '828540',
+ '832014',
+ ],
+ invalid: [
+ '+68189032',
+ '123456789',
+ '+681723845987',
+ '022452389',
+ '+681772345678',
+ '+681700456794',
+
+ ],
+ },
{
locale: 'ka-GE',
valid: [
From 698f4e64a1c6b8e0f1d05b8329b0b3184595a3d2 Mon Sep 17 00:00:00 2001
From: David Siegers
Date: Mon, 27 Mar 2023 11:59:26 +0200
Subject: [PATCH 2/4] fix(isVAT): corrected validation for Swiss (CH) locale
(#2203)
* correct isVAT regex for CH
* use checkdigit for swiss isvat
* allow space as number separator, edit test cases
* fix: correct test-case, edit comment
* extract vat matcher for Switzerland (CH)
- adjust number separation rule
- edit comments in test
---
src/lib/isVAT.js | 17 ++++++++++++++++-
test/validators.test.js | 36 ++++++++++++++++++++++++------------
2 files changed, 40 insertions(+), 13 deletions(-)
diff --git a/src/lib/isVAT.js b/src/lib/isVAT.js
index 95593569d..ece7d8560 100644
--- a/src/lib/isVAT.js
+++ b/src/lib/isVAT.js
@@ -1,6 +1,21 @@
import assertString from './util/assertString';
import * as algorithms from './util/algorithms';
+const CH = (str) => {
+ // @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0}
+ const hasValidCheckNumber = (digits) => {
+ const lastDigit = digits.pop(); // used as check number
+ const weights = [5, 4, 3, 2, 7, 6, 5, 4];
+ const calculatedCheckNumber = (11 - (digits.reduce((acc, el, idx) =>
+ acc + (el * weights[idx]), 0) % 11)) % 11;
+
+ return lastDigit === calculatedCheckNumber;
+ };
+
+ // @see {@link https://www.estv.admin.ch/estv/de/home/mehrwertsteuer/uid/mwst-uid-nummer.html}
+ return /^(CHE[- ]?)?(\d{9}|(\d{3}\.\d{3}\.\d{3})|(\d{3} \d{3} \d{3})) ?(TVA|MWST|IVA)?$/.test(str) && hasValidCheckNumber((str.match(/\d/g).map(el => +el)));
+};
+
const PT = (str) => {
const match = str.match(/^(PT)?(\d{9})$/);
if (!match) {
@@ -69,7 +84,7 @@ export const vatMatchers = {
SM: str => /^(SM)?\d{5}$/.test(str),
SA: str => /^(SA)?\d{15}$/.test(str),
RS: str => /^(RS)?\d{9}$/.test(str),
- CH: str => /^(CH)?(\d{6}|\d{9}|(\d{3}.\d{3})|(\d{3}.\d{3}.\d{3}))(TVA|MWST|IVA)$/.test(str),
+ CH,
TR: str => /^(TR)?\d{10}$/.test(str),
UA: str => /^(UA)?\d{12}$/.test(str),
GB: str => /^GB((\d{3} \d{4} ([0-8][0-9]|9[0-6]))|(\d{9} \d{3})|(((GD[0-4])|(HA[5-9]))[0-9]{2}))$/.test(str),
diff --git a/test/validators.test.js b/test/validators.test.js
index b4888457b..b6ff0d9f9 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -13891,18 +13891,30 @@ describe('Validators', () => {
validator: 'isVAT',
args: ['CH'],
valid: [
- 'CH123456TVA',
- '123456TVA',
- 'CH123456789MWST',
- '123456789MWST',
- 'CH123.456IVA',
- '123.456IVA',
- 'CH123.456.789TVA',
- '123.456.789TVA',
- ],
- invalid: [
- 'CH 123456',
- '12345',
+ // strictly valid
+ 'CHE-116.281.710 MWST',
+ 'CHE-116.281.710 IVA',
+ 'CHE-116.281.710 TVA',
+ // loosely valid presentation variants
+ 'CHE 116 281 710 IVA', // all separators are spaces
+ 'CHE-191.398.369MWST', // no space before suffix
+ 'CHE-116281710 MWST', // no number separators
+ 'CHE-116281710MWST', // no number separators and no space before suffix
+ 'CHE105854263MWST', // no separators
+ 'CHE-116.285.524', // no suffix (vat abbreviation)
+ 'CHE116281710', // no suffix and separators
+ '116.281.710 TVA', // no prefix (CHE, ISO-3166-1 Alpha-3)
+ '116281710MWST', // no prefix and separators
+ '100.218.485', // no prefix and suffix
+ '123456788', // no prefix, separators and suffix
+ ],
+ invalid: [
+ 'CH-116.281.710 MWST', // invalid prefix (should be CHE)
+ 'CHE-116.281 MWST', // invalid number of digits (should be 9)
+ 'CHE-123.456.789 MWST', // invalid last digit (should match the calculated check-number 8)
+ 'CHE-123.356.780 MWST', // invalid check-number (there are no swiss UIDs with the calculated check number 10)
+ 'CH-116.281.710 VAT', // invalid suffix (should be MWST, IVA or TVA)
+ 'CHE-116/281/710 IVA', // invalid number separators (should be all dots or all spaces)
],
});
test({
From fc49ad74ef3e8068e0c014cafd221df698cd869b Mon Sep 17 00:00:00 2001
From: Wahome
Date: Mon, 27 Mar 2023 13:01:25 +0300
Subject: [PATCH 3/4] new validator: isLocale, add support for validation of
more valid language tags (#2189)
Co-authored-by: Wahome Macharia
---
src/lib/isLocale.js | 110 ++++++++++++++++++++++++++++++++++++++--
test/validators.test.js | 35 +++++++++++++
2 files changed, 140 insertions(+), 5 deletions(-)
diff --git a/src/lib/isLocale.js b/src/lib/isLocale.js
index cacac8aec..ec84c8fce 100644
--- a/src/lib/isLocale.js
+++ b/src/lib/isLocale.js
@@ -1,11 +1,111 @@
import assertString from './util/assertString';
-const localeReg = /^[A-Za-z]{2,4}([_-]([A-Za-z]{4}|[\d]{3}))?([_-]([A-Za-z]{2}|[\d]{3}))?$/;
+/*
+ = 3ALPHA ; selected ISO 639 codes
+ *2("-" 3ALPHA) ; permanently reserved
+ */
+const extlang = '([A-Za-z]{3}(-[A-Za-z]{3}){0,2})';
+
+/*
+ = 2*3ALPHA ; shortest ISO 639 code
+ ["-" extlang] ; sometimes followed by
+ ; extended language subtags
+ / 4ALPHA ; or reserved for future use
+ / 5*8ALPHA ; or registered language subtag
+ */
+const language = `(([a-zA-Z]{2,3}(-${extlang})?)|([a-zA-Z]{5,8}))`;
+
+/*
+ = 4ALPHA ; ISO 15924 code
+ */
+const script = '([A-Za-z]{4})';
+
+/*
+ = 2ALPHA ; ISO 3166-1 code
+ / 3DIGIT ; UN M.49 code
+ */
+const region = '([A-Za-z]{2}|\\d{3})';
+
+/*
+ = 5*8alphanum ; registered variants
+ / (DIGIT 3alphanum)
+ */
+const variant = '([A-Za-z0-9]{5,8}|(\\d[A-Z-a-z0-9]{3}))';
+
+/*
+ = DIGIT ; 0 - 9
+ / %x41-57 ; A - W
+ / %x59-5A ; Y - Z
+ / %x61-77 ; a - w
+ / %x79-7A ; y - z
+ */
+const singleton = '(\\d|[A-W]|[Y-Z]|[a-w]|[y-z])';
+
+/*
+ = singleton 1*("-" (2*8alphanum))
+ ; Single alphanumerics
+ ; "x" reserved for private use
+ */
+const extension = `(${singleton}(-[A-Za-z0-9]{2,8})+)`;
+
+/*
+ = "x" 1*("-" (1*8alphanum))
+ */
+const privateuse = '(x(-[A-Za-z0-9]{1,8})+)';
+
+// irregular tags do not match the 'langtag' production and would not
+// otherwise be considered 'well-formed'. These tags are all valid, but
+// most are deprecated in favor of more modern subtags or subtag combination
+
+const irregular = '((en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|' +
+ '(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|' +
+ '(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE))';
+
+// regular tags match the 'langtag' production, but their subtags are not
+// extended language or variant subtags: their meaning is defined by
+// their registration and all of these are deprecated in favor of a more
+// modern subtag or sequence of subtags
+
+const regular = '((art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|' +
+ '(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang))';
+
+/*
+ = irregular ; non-redundant tags registered
+ / regular ; during the RFC 3066 era
+
+ */
+const grandfathered = `(${irregular}|${regular})`;
+
+/*
+ RFC 5646 defines delimitation of subtags via a hyphen:
+
+ "Subtag" refers to a specific section of a tag, delimited by a
+ hyphen, such as the subtags 'zh', 'Hant', and 'CN' in the tag "zh-
+ Hant-CN". Examples of subtags in this document are enclosed in
+ single quotes ('Hant')
+
+ However, we need to add "_" to maintain the existing behaviour.
+ */
+const delimiter = '(-|_)';
+
+/*
+ = language
+ ["-" script]
+ ["-" region]
+ *("-" variant)
+ *("-" extension)
+ ["-" privateuse]
+ */
+const langtag = `${language}(${delimiter}${script})?(${delimiter}${region})?(${delimiter}${variant})*(${delimiter}${extension})*(${delimiter}${privateuse})?`;
+
+/*
+ Regex implementation based on BCP RFC 5646
+ Tags for Identifying Languages
+ https://www.rfc-editor.org/rfc/rfc5646.html
+ */
+const languageTagRegex = new RegExp(`(^${privateuse}$)|(^${grandfathered}$)|(^${langtag}$)`);
export default function isLocale(str) {
assertString(str);
- if (str === 'en_US_POSIX' || str === 'ca_ES_VALENCIA') {
- return true;
- }
- return localeReg.test(str);
+ return languageTagRegex.test(str);
}
diff --git a/test/validators.test.js b/test/validators.test.js
index b6ff0d9f9..ad2ea70fc 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -4816,16 +4816,51 @@ describe('Validators', () => {
'uz_Latn_UZ',
'en',
'gsw',
+ 'en-US',
'es_ES',
+ 'es-419',
'sw_KE',
'am_ET',
+ 'zh-CHS',
'ca_ES_VALENCIA',
'en_US_POSIX',
+ 'hak-CN',
+ 'zh-Hant',
+ 'zh-Hans',
+ 'sr-Cyrl',
+ 'sr-Latn',
+ 'zh-cmn-Hans-CN',
+ 'cmn-Hans-CN',
+ 'zh-yue-HK',
+ 'yue-HK',
+ 'zh-Hans-CN',
+ 'sr-Latn-RS',
+ 'sl-rozaj',
+ 'sl-rozaj-biske',
+ 'sl-nedis',
+ 'de-CH-1901',
+ 'sl-IT-nedis',
+ 'hy-Latn-IT-arevela',
+ 'i-enochian',
+ 'en-scotland-fonipa',
+ 'sl-IT-rozaj-biske-1994',
+ 'de-CH-x-phonebk',
+ 'az-Arab-x-AZE-derbend',
+ 'x-whatever',
+ 'qaa-Qaaa-QM-x-southern',
+ 'de-Qaaa',
+ 'sr-Latn-QM',
+ 'sr-Qaaa-RS',
+ 'en-US-u-islamcal',
+ 'zh-CN-a-myext-x-private',
+ 'en-a-myext-b-another',
],
invalid: [
'lo_POP',
'12',
'12_DD',
+ 'de-419-DE',
+ 'a-DE',
],
});
});
From 9ba173563c8e2ef674c7aa1b67b283ef75508a3b Mon Sep 17 00:00:00 2001
From: Utpal Sarkar <19898129+uksarkar@users.noreply.github.com>
Date: Mon, 27 Mar 2023 16:04:12 +0600
Subject: [PATCH 4/4] new validator: isMailtoURI, validate the mailto link URI
format (#2188)
---
README.md | 2 ++
src/index.js | 2 ++
src/lib/isMailtoURI.js | 67 +++++++++++++++++++++++++++++++++++++++++
test/validators.test.js | 51 +++++++++++++++++++++++++++++++
4 files changed, 122 insertions(+)
create mode 100644 src/lib/isMailtoURI.js
diff --git a/README.md b/README.md
index af22345ef..5a33d329b 100644
--- a/README.md
+++ b/README.md
@@ -148,6 +148,7 @@ Validator | Description
**isLuhnNumber(str)** | check if the string passes the [Luhn algorithm check](https://en.wikipedia.org/wiki/Luhn_algorithm).
**isMACAddress(str [, options])** | check if the string is a MAC address.
`options` is an object which defaults to `{ no_separators: false }`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g. '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64.
**isMagnetURI(str)** | check if the string is a [Magnet URI format][Magnet URI Format].
+**isMailtoURI(str, [, options])** | check if the string is a [Magnet URI format][Mailto URI Format].
`options` is an object of validating emails inside the URI (check `isEmail`s options for details).
**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][MIME 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-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', '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`.
@@ -301,6 +302,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number
[Luhn Check]: https://en.wikipedia.org/wiki/Luhn_algorithm
[Magnet URI Format]: https://en.wikipedia.org/wiki/Magnet_URI_scheme
+[Mailto URI Format]: https://en.wikipedia.org/wiki/Mailto
[MIME Type]: https://en.wikipedia.org/wiki/Media_type
[mongoid]: http://docs.mongodb.org/manual/reference/object-id/
[RFC 3339]: https://tools.ietf.org/html/rfc3339
diff --git a/src/index.js b/src/index.js
index 07cbbefbe..fd97dc3ac 100644
--- a/src/index.js
+++ b/src/index.js
@@ -101,6 +101,7 @@ import isBase58 from './lib/isBase58';
import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';
import isMagnetURI from './lib/isMagnetURI';
+import isMailtoURI from './lib/isMailtoURI';
import isMimeType from './lib/isMimeType';
@@ -213,6 +214,7 @@ const validator = {
isBase64,
isDataURI,
isMagnetURI,
+ isMailtoURI,
isMimeType,
isLatLong,
ltrim,
diff --git a/src/lib/isMailtoURI.js b/src/lib/isMailtoURI.js
new file mode 100644
index 000000000..0dd95b6a9
--- /dev/null
+++ b/src/lib/isMailtoURI.js
@@ -0,0 +1,67 @@
+import trim from './trim';
+import isEmail from './isEmail';
+import assertString from './util/assertString';
+
+function parseMailtoQueryString(queryString) {
+ const allowedParams = new Set(['subject', 'body', 'cc', 'bcc']),
+ query = { cc: '', bcc: '' };
+ let isParseFailed = false;
+
+ const queryParams = queryString.split('&');
+
+ if (queryParams.length > 4) {
+ return false;
+ }
+
+ for (const q of queryParams) {
+ const [key, value] = q.split('=');
+
+ // checked for invalid and duplicated query params
+ if (key && !allowedParams.has(key)) {
+ isParseFailed = true;
+ break;
+ }
+
+ if (value && (key === 'cc' || key === 'bcc')) {
+ query[key] = value;
+ }
+
+ if (key) {
+ allowedParams.delete(key);
+ }
+ }
+
+ return isParseFailed ? false : query;
+}
+
+export default function isMailtoURI(url, options) {
+ assertString(url);
+
+ if (url.indexOf('mailto:') !== 0) {
+ return false;
+ }
+
+ const [to = '', queryString = ''] = url.replace('mailto:', '').split('?');
+
+ if (!to && !queryString) {
+ return true;
+ }
+
+ const query = parseMailtoQueryString(queryString);
+
+ if (!query) {
+ return false;
+ }
+
+ return `${to},${query.cc},${query.bcc}`
+ .split(',')
+ .every((email) => {
+ email = trim(email, ' ');
+
+ if (email) {
+ return isEmail(email, options);
+ }
+
+ return true;
+ });
+}
diff --git a/test/validators.test.js b/test/validators.test.js
index ad2ea70fc..937b52a26 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -14253,4 +14253,55 @@ describe('Validators', () => {
],
});
});
+ it('should validate mailto URI', () => {
+ test({
+ validator: 'isMailtoURI',
+ valid: [
+ 'mailto:?subject=something&cc=valid@mail.com',
+ 'mailto:?subject=something&cc=valid@mail.com,another@mail.com,',
+ 'mailto:?subject=something&bcc=valid@mail.com',
+ 'mailto:?subject=something&bcc=valid@mail.com,another@mail.com',
+ 'mailto:?bcc=valid@mail.com,another@mail.com',
+ 'mailto:?cc=valid@mail.com,another@mail.com',
+ 'mailto:?cc=valid@mail.com',
+ 'mailto:?bcc=valid@mail.com',
+ 'mailto:?subject=something&body=something else',
+ 'mailto:?subject=something&body=something else&cc=hello@mail.com,another@mail.com',
+ 'mailto:?subject=something&body=something else&bcc=hello@mail.com,another@mail.com',
+ 'mailto:?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com',
+ 'mailto:hello@mail.com',
+ 'mailto:info@mail.com?',
+ 'mailto:hey@mail.com?subject=something',
+ 'mailto:info@mail.com?subject=something&cc=valid@mail.com',
+ 'mailto:info@mail.com?subject=something&cc=valid@mail.com,another@mail.com,',
+ 'mailto:info@mail.com?subject=something&bcc=valid@mail.com',
+ 'mailto:info@mail.com?subject=something&bcc=valid@mail.com,another@mail.com',
+ 'mailto:info@mail.com?bcc=valid@mail.com,another@mail.com',
+ 'mailto:info@mail.com?cc=valid@mail.com,another@mail.com',
+ 'mailto:info@mail.com?cc=valid@mail.com',
+ 'mailto:info@mail.com?bcc=valid@mail.com&',
+ 'mailto:info@mail.com?subject=something&body=something else',
+ 'mailto:info@mail.com?subject=something&body=something else&cc=hello@mail.com,another@mail.com',
+ 'mailto:info@mail.com?subject=something&body=something else&bcc=hello@mail.com,another@mail.com',
+ 'mailto:info@mail.com?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com',
+ 'mailto:',
+ ],
+ invalid: [
+ '',
+ 'somthing',
+ 'valid@gmail.com',
+ 'mailto:?subject=okay&subject=444',
+ 'mailto:?subject=something&wrong=888',
+ 'mailto:somename@gmail.com',
+ 'mailto:hello@world.com?cc=somename@gmail.com',
+ 'mailto:hello@world.com?bcc=somename@gmail.com',
+ 'mailto:hello@world.com?bcc=somename@gmail.com&bcc',
+ 'mailto:valid@gmail.com?subject=anything&body=nothing&cc=&bcc=&key=',
+ 'mailto:hello@world.com?cc=somename',
+ 'mailto:somename',
+ 'mailto:info@mail.com?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com&',
+ 'mailto:?subject=something&body=something else&cc=something@mail.com&bcc=hello@mail.com,another@mail.com&',
+ ],
+ });
+ });
});