Skip to content

Commit 83ae52c

Browse files
authored
Merge branch 'validatorjs:master' into fix-isPort
2 parents fcbe17a + 9ba1735 commit 83ae52c

File tree

7 files changed

+330
-19
lines changed

7 files changed

+330
-19
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ Validator | Description
148148
**isLuhnNumber(str)** | check if the string passes the [Luhn algorithm check](https://en.wikipedia.org/wiki/Luhn_algorithm).
149149
**isMACAddress(str [, options])** | check if the string is a MAC address.<br/><br/>`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.
150150
**isMagnetURI(str)** | check if the string is a [Magnet URI format][Magnet URI Format].
151+
**isMailtoURI(str, [, options])** | check if the string is a [Magnet URI format][Mailto URI Format].<br/><br/>`options` is an object of validating emails inside the URI (check `isEmail`s options for details).
151152
**isMD5(str)** | check if the string is a MD5 hash.<br/><br/>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).
152153
**isMimeType(str)** | check if the string matches to a valid [MIME type][MIME Type] format.
153-
**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,<br/><br/>`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).<br/><br/>`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`.
154+
**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,<br/><br/>`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).<br/><br/>`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`.
154155
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
155156
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
156157
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`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 `.`).<br/><br/>`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']`.
@@ -301,6 +302,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
301302
[ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number
302303
[Luhn Check]: https://en.wikipedia.org/wiki/Luhn_algorithm
303304
[Magnet URI Format]: https://en.wikipedia.org/wiki/Magnet_URI_scheme
305+
[Mailto URI Format]: https://en.wikipedia.org/wiki/Mailto
304306
[MIME Type]: https://en.wikipedia.org/wiki/Media_type
305307
[mongoid]: http://docs.mongodb.org/manual/reference/object-id/
306308
[RFC 3339]: https://tools.ietf.org/html/rfc3339

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import isBase58 from './lib/isBase58';
101101
import isBase64 from './lib/isBase64';
102102
import isDataURI from './lib/isDataURI';
103103
import isMagnetURI from './lib/isMagnetURI';
104+
import isMailtoURI from './lib/isMailtoURI';
104105

105106
import isMimeType from './lib/isMimeType';
106107

@@ -213,6 +214,7 @@ const validator = {
213214
isBase64,
214215
isDataURI,
215216
isMagnetURI,
217+
isMailtoURI,
216218
isMimeType,
217219
isLatLong,
218220
ltrim,

src/lib/isLocale.js

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,111 @@
11
import assertString from './util/assertString';
22

3-
const localeReg = /^[A-Za-z]{2,4}([_-]([A-Za-z]{4}|[\d]{3}))?([_-]([A-Za-z]{2}|[\d]{3}))?$/;
3+
/*
4+
= 3ALPHA ; selected ISO 639 codes
5+
*2("-" 3ALPHA) ; permanently reserved
6+
*/
7+
const extlang = '([A-Za-z]{3}(-[A-Za-z]{3}){0,2})';
8+
9+
/*
10+
= 2*3ALPHA ; shortest ISO 639 code
11+
["-" extlang] ; sometimes followed by
12+
; extended language subtags
13+
/ 4ALPHA ; or reserved for future use
14+
/ 5*8ALPHA ; or registered language subtag
15+
*/
16+
const language = `(([a-zA-Z]{2,3}(-${extlang})?)|([a-zA-Z]{5,8}))`;
17+
18+
/*
19+
= 4ALPHA ; ISO 15924 code
20+
*/
21+
const script = '([A-Za-z]{4})';
22+
23+
/*
24+
= 2ALPHA ; ISO 3166-1 code
25+
/ 3DIGIT ; UN M.49 code
26+
*/
27+
const region = '([A-Za-z]{2}|\\d{3})';
28+
29+
/*
30+
= 5*8alphanum ; registered variants
31+
/ (DIGIT 3alphanum)
32+
*/
33+
const variant = '([A-Za-z0-9]{5,8}|(\\d[A-Z-a-z0-9]{3}))';
34+
35+
/*
36+
= DIGIT ; 0 - 9
37+
/ %x41-57 ; A - W
38+
/ %x59-5A ; Y - Z
39+
/ %x61-77 ; a - w
40+
/ %x79-7A ; y - z
41+
*/
42+
const singleton = '(\\d|[A-W]|[Y-Z]|[a-w]|[y-z])';
43+
44+
/*
45+
= singleton 1*("-" (2*8alphanum))
46+
; Single alphanumerics
47+
; "x" reserved for private use
48+
*/
49+
const extension = `(${singleton}(-[A-Za-z0-9]{2,8})+)`;
50+
51+
/*
52+
= "x" 1*("-" (1*8alphanum))
53+
*/
54+
const privateuse = '(x(-[A-Za-z0-9]{1,8})+)';
55+
56+
// irregular tags do not match the 'langtag' production and would not
57+
// otherwise be considered 'well-formed'. These tags are all valid, but
58+
// most are deprecated in favor of more modern subtags or subtag combination
59+
60+
const irregular = '((en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|' +
61+
'(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|' +
62+
'(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE))';
63+
64+
// regular tags match the 'langtag' production, but their subtags are not
65+
// extended language or variant subtags: their meaning is defined by
66+
// their registration and all of these are deprecated in favor of a more
67+
// modern subtag or sequence of subtags
68+
69+
const regular = '((art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|' +
70+
'(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang))';
71+
72+
/*
73+
= irregular ; non-redundant tags registered
74+
/ regular ; during the RFC 3066 era
75+
76+
*/
77+
const grandfathered = `(${irregular}|${regular})`;
78+
79+
/*
80+
RFC 5646 defines delimitation of subtags via a hyphen:
81+
82+
"Subtag" refers to a specific section of a tag, delimited by a
83+
hyphen, such as the subtags 'zh', 'Hant', and 'CN' in the tag "zh-
84+
Hant-CN". Examples of subtags in this document are enclosed in
85+
single quotes ('Hant')
86+
87+
However, we need to add "_" to maintain the existing behaviour.
88+
*/
89+
const delimiter = '(-|_)';
90+
91+
/*
92+
= language
93+
["-" script]
94+
["-" region]
95+
*("-" variant)
96+
*("-" extension)
97+
["-" privateuse]
98+
*/
99+
const langtag = `${language}(${delimiter}${script})?(${delimiter}${region})?(${delimiter}${variant})*(${delimiter}${extension})*(${delimiter}${privateuse})?`;
100+
101+
/*
102+
Regex implementation based on BCP RFC 5646
103+
Tags for Identifying Languages
104+
https://www.rfc-editor.org/rfc/rfc5646.html
105+
*/
106+
const languageTagRegex = new RegExp(`(^${privateuse}$)|(^${grandfathered}$)|(^${langtag}$)`);
4107

5108
export default function isLocale(str) {
6109
assertString(str);
7-
if (str === 'en_US_POSIX' || str === 'ca_ES_VALENCIA') {
8-
return true;
9-
}
10-
return localeReg.test(str);
110+
return languageTagRegex.test(str);
11111
}

src/lib/isMailtoURI.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import trim from './trim';
2+
import isEmail from './isEmail';
3+
import assertString from './util/assertString';
4+
5+
function parseMailtoQueryString(queryString) {
6+
const allowedParams = new Set(['subject', 'body', 'cc', 'bcc']),
7+
query = { cc: '', bcc: '' };
8+
let isParseFailed = false;
9+
10+
const queryParams = queryString.split('&');
11+
12+
if (queryParams.length > 4) {
13+
return false;
14+
}
15+
16+
for (const q of queryParams) {
17+
const [key, value] = q.split('=');
18+
19+
// checked for invalid and duplicated query params
20+
if (key && !allowedParams.has(key)) {
21+
isParseFailed = true;
22+
break;
23+
}
24+
25+
if (value && (key === 'cc' || key === 'bcc')) {
26+
query[key] = value;
27+
}
28+
29+
if (key) {
30+
allowedParams.delete(key);
31+
}
32+
}
33+
34+
return isParseFailed ? false : query;
35+
}
36+
37+
export default function isMailtoURI(url, options) {
38+
assertString(url);
39+
40+
if (url.indexOf('mailto:') !== 0) {
41+
return false;
42+
}
43+
44+
const [to = '', queryString = ''] = url.replace('mailto:', '').split('?');
45+
46+
if (!to && !queryString) {
47+
return true;
48+
}
49+
50+
const query = parseMailtoQueryString(queryString);
51+
52+
if (!query) {
53+
return false;
54+
}
55+
56+
return `${to},${query.cc},${query.bcc}`
57+
.split(',')
58+
.every((email) => {
59+
email = trim(email, ' ');
60+
61+
if (email) {
62+
return isEmail(email, options);
63+
}
64+
65+
return true;
66+
});
67+
}

src/lib/isMobilePhone.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const phones = {
104104
'fr-MQ': /^(\+?596|0|00596)[67]\d{8}$/,
105105
'fr-PF': /^(\+?689)?8[789]\d{6}$/,
106106
'fr-RE': /^(\+?262|0|00262)[67]\d{8}$/,
107+
'fr-WF': /^(\+681)?\d{6}$/,
107108
'he-IL': /^(\+972|0)([23489]|5[012345689]|77)[1-9]\d{6}$/,
108109
'hu-HU': /^(\+?36|06)(20|30|31|50|70)\d{7}$/,
109110
'id-ID': /^(\+?62|0)8(1[123456789]|2[1238]|3[1238]|5[12356789]|7[78]|9[56789]|8[123456789])([\s?|\d]{5,11})$/,

src/lib/isVAT.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import assertString from './util/assertString';
22
import * as algorithms from './util/algorithms';
33

4+
const CH = (str) => {
5+
// @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0}
6+
const hasValidCheckNumber = (digits) => {
7+
const lastDigit = digits.pop(); // used as check number
8+
const weights = [5, 4, 3, 2, 7, 6, 5, 4];
9+
const calculatedCheckNumber = (11 - (digits.reduce((acc, el, idx) =>
10+
acc + (el * weights[idx]), 0) % 11)) % 11;
11+
12+
return lastDigit === calculatedCheckNumber;
13+
};
14+
15+
// @see {@link https://www.estv.admin.ch/estv/de/home/mehrwertsteuer/uid/mwst-uid-nummer.html}
16+
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)));
17+
};
18+
419
const PT = (str) => {
520
const match = str.match(/^(PT)?(\d{9})$/);
621
if (!match) {
@@ -69,7 +84,7 @@ export const vatMatchers = {
6984
SM: str => /^(SM)?\d{5}$/.test(str),
7085
SA: str => /^(SA)?\d{15}$/.test(str),
7186
RS: str => /^(RS)?\d{9}$/.test(str),
72-
CH: str => /^(CH)?(\d{6}|\d{9}|(\d{3}.\d{3})|(\d{3}.\d{3}.\d{3}))(TVA|MWST|IVA)$/.test(str),
87+
CH,
7388
TR: str => /^(TR)?\d{10}$/.test(str),
7489
UA: str => /^(UA)?\d{12}$/.test(str),
7590
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),

0 commit comments

Comments
 (0)