Repository: validatorjs/validator.js (js)

Issue:
isBase64("") returns false
The string "" is valid base64 (see first example https://tools.ietf.org/html/rfc4648#section-10) but `isBase64("")` returns `false`. This is also errorneous in the tests (see https://github.com/validatorjs/validator.js/blob/master/test/validators.js line 4755).

_Originally posted by @AberDerBart in https://github.com/validatorjs/validator.js/issues/1239#issuecomment-672764860_

Pull request:
fix isBase64 and isBase32 seeing empty string as invalid, fixes #1418
<!--
Add a descriptive title textbox above, e.g.
feat(validatorName): brief title of what has been done
-->

isBase64('') and isBase32('') now return true, which is correct behaviour according to [RFC4648](https://tools.ietf.org/html/rfc4648#section-10)

Upon regeneration of the code, there were some additional files changes, I am not sure if this is right, so I made a seperate commit.

## Checklist

- [ ] PR contains only changes related; no stray files, etc.
- [x] README updated (where applicable)
- [x] Tests written (where applicable)


Patch:
diff --git a/README.md b/README.md
index d952b4d..7a7c72f 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.<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).
 **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,<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', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', '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-PH', '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).<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`.
+**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', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', '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-PH', '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', '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`.
 **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.<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', '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/isBase32.js b/src/lib/isBase32.js
index 1988ea9..b2e9c8a 100644
--- a/src/lib/isBase32.js
+++ b/src/lib/isBase32.js
@@ -5,7 +5,7 @@ const base32 = /^[A-Z2-7]+=*$/;
 export default function isBase32(str) {
   assertString(str);
   const len = str.length;
-  if (len > 0 && len % 8 === 0 && base32.test(str)) {
+  if (len % 8 === 0 && base32.test(str)) {
     return true;
   }
   return false;
diff --git a/src/lib/isBase64.js b/src/lib/isBase64.js
index 6bfe031..02dead0 100644
--- a/src/lib/isBase64.js
+++ b/src/lib/isBase64.js
@@ -2,7 +2,7 @@ import assertString from './util/assertString';
 import merge from './util/merge';
 
 const notBase64 = /[^A-Z0-9+\/=]/i;
-const urlSafeBase64 = /^[A-Z0-9_\-]+$/i;
+const urlSafeBase64 = /^[A-Z0-9_\-]*$/i;
 
 const defaultBase64Options = {
   urlSafe: false,
@@ -17,7 +17,7 @@ export default function isBase64(str, options) {
     return urlSafeBase64.test(str);
   }
 
-  if (!len || len % 4 !== 0 || notBase64.test(str)) {
+  if (len % 4 !== 0 || notBase64.test(str)) {
     return false;
   }
 
diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js
index 52cb77e..987a127 100644
--- a/src/lib/isMobilePhone.js
+++ b/src/lib/isMobilePhone.js
@@ -94,6 +94,7 @@ const phones = {
   'th-TH': /^(\+66|66|0)\d{9}$/,
   'tr-TR': /^(\+?90|0)?5\d{9}$/,
   'uk-UA': /^(\+?38|8)?0\d{9}$/,
+  'uz-UZ': /^(\+?998)?(6[125-79]|7[1-69]|88|9\d)\d{7}$/,
   '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([3568][0-9]|4[579]|6[67]|7[01235678]|9[012356789])[0-9]{8}$/,
   'zh-TW': /^(\+?886\-?|0)?9\d{8}$/,
diff --git a/src/lib/isURL.js b/src/lib/isURL.js
index 6ca916f..a509b16 100644
--- a/src/lib/isURL.js
+++ b/src/lib/isURL.js
@@ -100,7 +100,7 @@ export default function isURL(url, options) {
       return false;
     }
     auth = split.shift();
-    if (auth.indexOf(':') >= 0 && auth.split(':').length > 2) {
+    if (auth.indexOf(':') === -1 || (auth.indexOf(':') >= 0 && auth.split(':').length > 2)) {
       return false;
     }
   }


Test patch:
diff --git a/test/validators.js b/test/validators.js
old mode 100755
new mode 100644
index 20460c8..4e85f8b
--- a/test/validators.js
+++ b/test/validators.js
@@ -339,6 +339,7 @@ describe('Validators', () => {
         'http://[::FFFF:129.144.52.38]:80/index.html',
         'http://[2010:836B:4179::836B:4179]',
         'http://example.com/example.json#/foo/bar',
+        'http://user:@www.foobar.com',
       ],
       invalid: [
         'http://localhost:3000/',
@@ -379,6 +380,7 @@ describe('Validators', () => {
         '////foobar.com',
         'http:////foobar.com',
         'https://example.com/foo/<script>alert(\'XSS\')</script>/',
+        'myemail@mail.com',
       ],
     });
   });
@@ -4733,6 +4735,7 @@ describe('Validators', () => {
     test({
       validator: 'isBase64',
       valid: [
+        '',
         'Zg==',
         'Zm8=',
         'Zm9v',
@@ -4752,7 +4755,6 @@ describe('Validators', () => {
       ],
       invalid: [
         '12345',
-        '',
         'Vml2YW11cyBmZXJtZtesting123',
         'Zg=',
         'Z===',
@@ -4766,6 +4768,7 @@ describe('Validators', () => {
       validator: 'isBase64',
       args: [{ urlSafe: true }],
       valid: [
+        '',
         'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
         '1234',
         'bXVtLW5ldmVyLXByb3Vk',
@@ -4776,7 +4779,6 @@ describe('Validators', () => {
         ' AA',
         '\tAA',
         '\rAA',
-        '',
         '\nAA',
         'This+isa/bad+base64Url==',
         '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
@@ -6599,6 +6601,29 @@ describe('Validators', () => {
           '740123456',
         ],
       },
+      {
+        locale: 'uz-UZ',
+        valid: [
+          '+998664835244',
+          '998664835244',
+          '664835244',
+          '+998957124555',
+          '998957124555',
+          '957124555',
+        ],
+        invalid: [
+          '+998644835244',
+          '998644835244',
+          '644835244',
+          '+99664835244',
+          'ASDFGJKLmZXJtZtesting123',
+          '123456789',
+          '870123456',
+          '',
+          '+998',
+          '998',
+        ],
+      },
       {
         locale: 'da-DK',
         valid: [
@@ -8717,6 +8742,7 @@ describe('Validators', () => {
       validator: 'isBase64',
       args: [{ urlSafe: true }],
       valid: [
+        '',
         'bGFkaWVzIGFuZCBnZW50bGVtZW4sIHdlIGFyZSBmbG9hdGluZyBpbiBzcGFjZQ',
         '1234',
         'bXVtLW5ldmVyLXByb3Vk',
@@ -8729,7 +8755,6 @@ describe('Validators', () => {
         '\rAA',
         '\nAA',
         '123=',
-        '',
         'This+isa/bad+base64Url==',
         '0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw',
       ],