Skip to content

Commit 2fd8798

Browse files
authored
Merge branch 'master' into master
2 parents 363dd60 + 050a424 commit 2fd8798

File tree

6 files changed

+155
-17
lines changed

6 files changed

+155
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Validator | Description
107107
**isEmpty(str [, options])** | check if the string has a length of zero.<br/><br/>`options` is an object which defaults to `{ ignore_whitespace:false }`.
108108
**isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums.
109109
**isFloat(str [, options])** | check if the string is a float.<br/><br/>`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.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`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-CA', '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`.
110-
**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false , allow_numeric_tld: false }`.
110+
**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
111111
**isFullWidth(str)** | check if the string contains any full-width chars.
112112
**isHalfWidth(str)** | check if the string contains any half-width chars.
113113
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
@@ -140,7 +140,7 @@ Validator | Description
140140
**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
141141
**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).
142142
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
143-
**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-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'el-GR', 'en-AU', en-BM, '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-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'lt-LT', 'ms-MY', ''mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', 'sq-AL', 'sr-RS', 'sv-SE', 'th-TH', '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`.
143+
**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-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'el-GR', 'en-AU', 'en-BM', '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-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'lt-LT', 'ms-MY', ''mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', 'sq-AL', 'sr-RS', 'sv-SE', 'th-TH', '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`.
144144
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
145145
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
146146
**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` 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', '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']`.

src/lib/isFQDN.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const default_fqdn_options = {
66
allow_underscores: false,
77
allow_trailing_dot: false,
88
allow_numeric_tld: false,
9+
allow_wildcard: false,
910
};
1011

1112
export default function isFQDN(str, options) {
@@ -16,6 +17,12 @@ export default function isFQDN(str, options) {
1617
if (options.allow_trailing_dot && str[str.length - 1] === '.') {
1718
str = str.substring(0, str.length - 1);
1819
}
20+
21+
/* Remove the optional wildcard before checking validity */
22+
if (options.allow_wildcard === true && str.indexOf('*.') === 0) {
23+
str = str.substring(2);
24+
}
25+
1926
const parts = str.split('.');
2027
const tld = parts[parts.length - 1];
2128

src/lib/isMobilePhone.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const phones = {
4444
'en-MU': /^(\+?230|0)?\d{8}$/,
4545
'en-NG': /^(\+?234|0)?[789]\d{9}$/,
4646
'en-NZ': /^(\+?64|0)[28]\d{7,9}$/,
47-
'en-PK': /^((\+92)|(0092))-{0,1}\d{3}-{0,1}\d{7}$|^\d{11}$|^\d{4}-\d{7}$/,
47+
'en-PK': /^((00|\+)?92|0)3[0-6]\d{8}$/,
4848
'en-PH': /^(09|\+639)\d{9}$/,
4949
'en-RW': /^(\+?250|0)?[7]\d{8}$/,
5050
'en-SG': /^(\+65)?[3689]\d{7}$/,
@@ -60,6 +60,7 @@ const phones = {
6060
'es-CO': /^(\+?57)?3(0(0|1|2|4|5)|1\d|2[0-4]|5(0|1))\d{7}$/,
6161
'es-CL': /^(\+?56|0)[2-9]\d{1}\d{7}$/,
6262
'es-CR': /^(\+506)?[2-8]\d{7}$/,
63+
'es-CU': /^(\+53|0053)?5\d{7}/,
6364
'es-DO': /^(\+?1)?8[024]9\d{7}$/,
6465
'es-HN': /^(\+?504)?[9|8]\d{7}$/,
6566
'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/,
@@ -116,7 +117,7 @@ const phones = {
116117
'uk-UA': /^(\+?38|8)?0\d{9}$/,
117118
'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/,
118119
'vi-VN': /^((\+?84)|0)((3([2-9]))|(5([25689]))|(7([0|6-9]))|(8([1-9]))|(9([0-9])))([0-9]{7})$/,
119-
'zh-CN': /^((\+|00)86)?1([3456789][0-9]|4[579]|6[2567]|7[01235678]|9[012356789])[0-9]{8}$/,
120+
'zh-CN': /^((\+|00)86)?(1[3-9]|9[28])\d{9}$/,
120121
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/,
121122
};
122123
/* eslint-enable max-len */

src/lib/isURL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export default function isURL(url, options) {
136136
}
137137
}
138138

139-
if (port_str !== null) {
139+
if (port_str !== null && port_str.length > 0) {
140140
port = parseInt(port_str, 10);
141141
if (!/^[0-9]+$/.test(port_str) || port <= 0 || port > 65535) {
142142
return false;

src/lib/isUUID.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const uuid = {
77
all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
88
};
99

10-
export default function isUUID(str, version = 'all') {
10+
export default function isUUID(str, version) {
1111
assertString(str);
12-
const pattern = uuid[version];
12+
const pattern = uuid[![undefined, null].includes(version) ? version : 'all'];
1313
return pattern && pattern.test(str);
1414
}

test/validators.js

Lines changed: 140 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,31 @@ describe('Validators', () => {
535535
});
536536
});
537537

538+
it('should validate URLs with column and no port', () => {
539+
test({
540+
validator: 'isURL',
541+
valid: [
542+
'http://example.com:',
543+
'ftp://example.com:',
544+
],
545+
invalid: [
546+
'https://example.com:abc',
547+
],
548+
});
549+
});
550+
551+
it('should validate sftp protocol URL containing column and no port', () => {
552+
test({
553+
validator: 'isURL',
554+
args: [{
555+
protocols: ['sftp'],
556+
}],
557+
valid: [
558+
'sftp://user:[email protected]:/incoming/things.csv',
559+
],
560+
});
561+
});
562+
538563
it('should validate protocol relative URLs', () => {
539564
test({
540565
validator: 'isURL',
@@ -1118,6 +1143,18 @@ describe('Validators', () => {
11181143
],
11191144
});
11201145
});
1146+
it('should validate FQDN with wildcard option', () => {
1147+
test({
1148+
validator: 'isFQDN',
1149+
args: [
1150+
{ allow_wildcard: true },
1151+
],
1152+
valid: [
1153+
'*.example.com',
1154+
'*.shop.example.com',
1155+
],
1156+
});
1157+
});
11211158

11221159
it('should validate alpha strings', () => {
11231160
test({
@@ -4413,6 +4450,34 @@ describe('Validators', () => {
44134450
'AAAAAAAA-1111-1111-AAAG-111111111111',
44144451
],
44154452
});
4453+
test({
4454+
validator: 'isUUID',
4455+
args: [undefined],
4456+
valid: [
4457+
'A117FBC9-4BED-3078-CF07-9141BA07C9F3',
4458+
'A117FBC9-4BED-5078-AF07-9141BA07C9F3',
4459+
],
4460+
invalid: [
4461+
'',
4462+
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
4463+
'A987FBC94BED3078CF079141BA07C9F3',
4464+
'A11AAAAA-1111-1111-AAAG-111111111111',
4465+
],
4466+
});
4467+
test({
4468+
validator: 'isUUID',
4469+
args: [null],
4470+
valid: [
4471+
'A127FBC9-4BED-3078-CF07-9141BA07C9F3',
4472+
],
4473+
invalid: [
4474+
'',
4475+
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
4476+
'A127FBC9-4BED-3078-CF07-9141BA07C9F3xxx',
4477+
'912859',
4478+
'A12AAAAA-1111-1111-AAAG-111111111111',
4479+
],
4480+
});
44164481
test({
44174482
validator: 'isUUID',
44184483
args: [3],
@@ -6102,19 +6167,28 @@ describe('Validators', () => {
61026167
{
61036168
locale: 'zh-CN',
61046169
valid: [
6105-
'15323456787',
61066170
'13523333233',
6107-
'13898728332',
6171+
'13838389438',
6172+
'14899230918',
6173+
'14999230918',
6174+
'15323456787',
6175+
'15052052020',
6176+
'16237108167',
6177+
'008616238234822',
6178+
'+8616238234822',
6179+
'16565600001',
6180+
'17269427292',
6181+
'17469427292',
6182+
'18199617480',
6183+
'19151751717',
6184+
'19651751717',
61086185
'+8613238234822',
61096186
'+8613487234567',
61106187
'+8617823492338',
61116188
'+8617823492338',
6112-
'16637108167',
61136189
'+8616637108167',
61146190
'+8616637108167',
61156191
'+8616712341234',
6116-
'008618812341234',
6117-
'008618812341234',
61186192
'+8619912341234',
61196193
'+8619812341234',
61206194
'+8619712341234',
@@ -6123,17 +6197,25 @@ describe('Validators', () => {
61236197
'+8619312341234',
61246198
'+8619212341234',
61256199
'+8619112341234',
6126-
'17269427292',
6127-
'16565600001',
61286200
'+8617269427292',
6201+
'008618812341234',
6202+
'008618812341234',
61296203
'008617269427292',
6130-
'16238234822',
6131-
'008616238234822',
6132-
'+8616238234822',
6204+
// Reserve number segments in the future.
6205+
'92138389438',
6206+
'+8692138389438',
6207+
'008692138389438',
6208+
'98199649964',
6209+
'+8698099649964',
6210+
'008698099649964',
61336211
],
61346212
invalid: [
61356213
'12345',
61366214
'',
6215+
'12038389438',
6216+
'12838389438',
6217+
'013838389438',
6218+
'+86-13838389438',
61376219
'+08613811211114',
61386220
'+008613811211114',
61396221
'08613811211114',
@@ -7036,6 +7118,34 @@ describe('Validators', () => {
70367118
'01234567',
70377119
],
70387120
},
7121+
{
7122+
locale: 'es-CU',
7123+
valid: [
7124+
'+5351234567',
7125+
'005353216547',
7126+
'51234567',
7127+
'53214567',
7128+
],
7129+
invalid: [
7130+
'1234',
7131+
'+5341234567',
7132+
'0041234567',
7133+
'41234567',
7134+
'11234567',
7135+
'21234567',
7136+
'31234567',
7137+
'60303456',
7138+
'71234567',
7139+
'81234567',
7140+
'91234567',
7141+
'+5343216547',
7142+
'+5332165498',
7143+
'+53121234567',
7144+
'',
7145+
'abc',
7146+
'+535123457',
7147+
],
7148+
},
70397149
{
70407150
locale: 'es-DO',
70417151
valid: [
@@ -7966,6 +8076,26 @@ describe('Validators', () => {
79668076
'NotANumber',
79678077
],
79688078
},
8079+
{
8080+
locale: 'en-PK',
8081+
valid: [
8082+
'+923412877421',
8083+
'+923001234567',
8084+
'00923001234567',
8085+
'923001234567',
8086+
'03001234567',
8087+
],
8088+
invalid: [
8089+
'+3001234567',
8090+
'+933001234567',
8091+
'+924001234567',
8092+
'+92300123456720',
8093+
'030012345672',
8094+
'30012345673',
8095+
'0030012345673',
8096+
'3001234567',
8097+
],
8098+
},
79698099
];
79708100

79718101
let allValid = [];

0 commit comments

Comments
 (0)