Skip to content

Commit ed86b0a

Browse files
authored
fix(isURL): added validate_length option (#1397)
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
1 parent af36196 commit ed86b0a

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Validator | Description
151151
**isUppercase(str)** | check if the string is uppercase.
152152
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
153153
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`
154-
**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.
154+
**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).
155155
**isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5).
156156
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
157157
**isWhitelisted(str, chars)** | checks characters if they appear in the whitelist.

src/lib/isURL.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require_valid_protocol - isURL will check if the URL's protocol is present in th
1212
protocols - valid protocols can be modified with this option
1313
require_host - if set as false isURL will not check if host is present in the URL
1414
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
15+
validate_length - if set as false isURL will skip string length validation (IE maximum is 2083)
1516
1617
*/
1718

@@ -25,6 +26,7 @@ const default_url_options = {
2526
allow_underscores: false,
2627
allow_trailing_dot: false,
2728
allow_protocol_relative_urls: false,
29+
validate_length: true,
2830
};
2931

3032
const wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
@@ -45,13 +47,18 @@ function checkHost(host, matches) {
4547

4648
export default function isURL(url, options) {
4749
assertString(url);
48-
if (!url || url.length >= 2083 || /[\s<>]/.test(url)) {
50+
if (!url || /[\s<>]/.test(url)) {
4951
return false;
5052
}
5153
if (url.indexOf('mailto:') === 0) {
5254
return false;
5355
}
5456
options = merge(options, default_url_options);
57+
58+
if (options.validate_length && url.length >= 2083) {
59+
return false;
60+
}
61+
5562
let protocol, auth, host, hostname, port, port_str, split, ipv6;
5663

5764
split = url.split('#');

test/validators.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,18 @@ describe('Validators', () => {
629629
});
630630
});
631631

632+
it('should allow user to skip URL length validation', () => {
633+
test({
634+
validator: 'isURL',
635+
args: [{ validate_length: false }],
636+
valid: [
637+
'http://foobar.com/f',
638+
`http://foobar.com/${new Array(2083).join('f')}`,
639+
],
640+
invalid: [],
641+
});
642+
});
643+
632644
it('should validate MAC addresses', () => {
633645
test({
634646
validator: 'isMACAddress',

0 commit comments

Comments
 (0)