diff --git a/README.md b/README.md index b4bb4be63..b62a4e470 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.

`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 }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
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. +**isURL(str [, options])** | check if the string is an URL.

`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 }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
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 (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 c45b9abd4..6ca916fb2 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('#'); diff --git a/test/validators.js b/test/validators.js index 54f36489f..ab6128516 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',