Skip to content

Commit

Permalink
fix(isURL): added validate_length option (#1397)
Browse files Browse the repository at this point in the history
fixes #1396 

* add validate_length to isURL

fix for issue: #1396

* add to README.md

* revert

* moved logics to src/lib
added test

* fixed linting
  • Loading branch information
tomgrossman authored Aug 1, 2020
1 parent af36196 commit ed86b0a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/lib/isURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/

Expand All @@ -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]+))?$/;
Expand All @@ -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('#');
Expand Down
12 changes: 12 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ed86b0a

Please sign in to comment.