Skip to content

Commit 542cfba

Browse files
authored
feat(isURL): add max_allowed_length option (#2439)
1 parent 1f159ea commit 542cfba

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Validator | Description
166166
**isStrongPassword(str [, options])** | check if the string can be considered a strong password or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
167167
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFormat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
168168
**isTaxID(str, locale)** | check if the string is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`.<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-AR', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV', 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE', 'uk-UA']`.
169-
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`require_protocol` - if set to 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 to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`validate_length` - if set to false isURL will skip string length validation (2083 characters is IE max URL length).
169+
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`require_protocol` - if set to 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 to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.<br/>`max_allowed_length` - if set isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).
170170
**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec).
171171
**isUUID(str [, version])** | check if the string is an RFC9562 UUID.<br/>`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, or `'all'`.
172172
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.

src/lib/isURL.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ 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
require_port - if set as true isURL will check if port is present in the URL
1515
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
16-
validate_length - if set as false isURL will skip string length validation (IE maximum is 2083)
17-
16+
validate_length - if set as false isURL will skip string length validation
17+
max_allowed_length will be ignored if this is set as false
18+
max_allowed_length - if set isURL will not allow URLs longer than max_allowed_length
19+
default is 2084 that IE maximum URL length
1820
*/
1921

2022

@@ -31,6 +33,7 @@ const default_url_options = {
3133
allow_fragments: true,
3234
allow_query_components: true,
3335
validate_length: true,
36+
max_allowed_length: 2084,
3437
};
3538

3639
const wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
@@ -59,7 +62,7 @@ export default function isURL(url, options) {
5962
}
6063
options = merge(options, default_url_options);
6164

62-
if (options.validate_length && url.length >= 2083) {
65+
if (options.validate_length && url.length > options.max_allowed_length) {
6366
return false;
6467
}
6568

test/validators.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,21 @@ describe('Validators', () => {
787787
});
788788
});
789789

790+
it('should allow user to configure the maximum URL length', () => {
791+
test({
792+
validator: 'isURL',
793+
args: [{ max_allowed_length: 20 }],
794+
valid: [
795+
'http://foobar.com/12', // 20 characters
796+
'http://foobar.com/',
797+
],
798+
invalid: [
799+
'http://foobar.com/123', // 21 characters
800+
'http://foobar.com/1234567890',
801+
],
802+
});
803+
});
804+
790805
it('should validate URLs with port present', () => {
791806
test({
792807
validator: 'isURL',

0 commit comments

Comments
 (0)