Repository: validatorjs/validator.js
Pull request #1397 (2020-07-29 13:10:23)

add(isURL): added validate_length option
fix for issue: https://github.com/validatorjs/validator.js/issues/1396

Today the `isURL` function validate hard-coded for max 2083 string length. This is not part of the RFC and should be able to skip this validation.

## Checklist

- [x] PR contains only changes related; no stray files, etc.
- [x] README updated (where applicable)
- [ ] Tests written (where applicable)


tomgrossman: @tux-tn fixed. for some reason I can't click on the "re-request review" button, it doesn't work...

Patch:
diff --git a/README.md b/README.md
index b4bb4be..b62a4e4 100644
--- a/README.md
+++ b/README.md
@@ -151,7 +151,7 @@ Validator                               | Description
 **isUppercase(str)**                    | check if the string is uppercase.
 **isSlug**                              | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
 **isTaxID(str, locale)**                | Check if the given value is a valid Tax Identification    Number. Default locale is `en-US`
-**isURL(str [, options])**              | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.
+**isURL(str [, options])**              | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.<br/>validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length).
 **isUUID(str [, version])**             | check if the string is a UUID (version 3, 4 or 5).
 **isVariableWidth(str)**                | check if the string contains a mixture of full and half-width chars.
 **isWhitelisted(str, chars)**           | checks characters if they appear in the whitelist.
diff --git a/src/lib/isURL.js b/src/lib/isURL.js
index c45b9ab..6ca916f 100644
--- a/src/lib/isURL.js
+++ b/src/lib/isURL.js
@@ -12,6 +12,7 @@ require_valid_protocol - isURL will check if the URL's protocol is present in th
 protocols - valid protocols can be modified with this option
 require_host - if set as false isURL will not check if host is present in the URL
 allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
+validate_length - if set as false isURL will skip string length validation (IE maximum is 2083)
 
 */
 
@@ -25,6 +26,7 @@ const default_url_options = {
   allow_underscores: false,
   allow_trailing_dot: false,
   allow_protocol_relative_urls: false,
+  validate_length: true,
 };
 
 const wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
@@ -45,13 +47,18 @@ function checkHost(host, matches) {
 
 export default function isURL(url, options) {
   assertString(url);
-  if (!url || url.length >= 2083 || /[\s<>]/.test(url)) {
+  if (!url || /[\s<>]/.test(url)) {
     return false;
   }
   if (url.indexOf('mailto:') === 0) {
     return false;
   }
   options = merge(options, default_url_options);
+
+  if (options.validate_length && url.length >= 2083) {
+    return false;
+  }
+
   let protocol, auth, host, hostname, port, port_str, split, ipv6;
 
   split = url.split('#');


Test patch:
diff --git a/test/validators.js b/test/validators.js
index 54f3648..ab61285 100755
--- a/test/validators.js
+++ b/test/validators.js
@@ -629,6 +629,18 @@ describe('Validators', () => {
     });
   });
 
+  it('should allow user to skip URL length validation', () => {
+    test({
+      validator: 'isURL',
+      args: [{ validate_length: false }],
+      valid: [
+        'http://foobar.com/f',
+        `http://foobar.com/${new Array(2083).join('f')}`,
+      ],
+      invalid: [],
+    });
+  });
+
   it('should validate MAC addresses', () => {
     test({
       validator: 'isMACAddress',