Skip to content

Commit

Permalink
Add an option to allow no host, this closes #563
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Sep 11, 2016
1 parent b7c749b commit 2bf79a5
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Added support for IPv6 in `isURL()`
([#564](https://github.com/chriso/validator.js/issues/564))
- Added support for urls without a host (e.g. `file:///foo.txt`) in `isURL()`
([#563](https://github.com/chriso/validator.js/issues/563))
- Added support for MasterCard 2-Series BIN
([#576](https://github.com/chriso/validator.js/pull/576))
- New locales
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Passing anything other than a string is an error.
- **isNull(str)** - check if the string is null (has a length of zero).
- **isNumeric(str)** - check if the string contains only numbers.
- **isSurrogatePair(str)** - check if the string contains any surrogate pairs chars.
- **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_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }`.
- **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 }`.
- **isUUID(str [, version])** - check if the string is a UUID (version 3, 4 or 5).
- **isUppercase(str)** - check if the string is uppercase.
- **isVariableWidth(str)** - check if the string contains a mixture of full and half-width chars.
Expand Down
6 changes: 6 additions & 0 deletions lib/isURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var default_url_options = {
protocols: ['http', 'https', 'ftp'],
require_tld: true,
require_protocol: false,
require_host: true,
require_valid_protocol: true,
allow_underscores: false,
allow_trailing_dot: false,
Expand Down Expand Up @@ -74,6 +75,11 @@ function isURL(url, options) {

split = url.split('/');
url = split.shift();

if (url === '' && !options.require_host) {
return true;
}

split = url.split('@');
if (split.length > 1) {
auth = split.shift();
Expand Down
6 changes: 6 additions & 0 deletions src/lib/isURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const default_url_options = {
protocols: ['http', 'https', 'ftp'],
require_tld: true,
require_protocol: false,
require_host: true,
require_valid_protocol: true,
allow_underscores: false,
allow_trailing_dot: false,
Expand Down Expand Up @@ -48,6 +49,11 @@ export default function isURL(url, options) {

split = url.split('/');
url = split.shift();

if (url === '' && !options.require_host) {
return true;
}

split = url.split('@');
if (split.length > 1) {
auth = split.shift();
Expand Down
18 changes: 18 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ describe('Validators', function () {
});
});

it('should validate file URLs without a host', function () {
test({
validator: 'isURL',
args: [{
protocols: ['file'],
require_host: false,
}],
valid: [
'file://localhost/foo.txt',
'file:///foo.txt',
'file:///',
],
invalid: [
'http://foobar.com',
],
});
});

it('should validate URLs with any protocol', function () {
test({
validator: 'isURL',
Expand Down
6 changes: 6 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
protocols: ['http', 'https', 'ftp'],
require_tld: true,
require_protocol: false,
require_host: true,
require_valid_protocol: true,
allow_underscores: false,
allow_trailing_dot: false,
Expand Down Expand Up @@ -342,6 +343,11 @@

split = url.split('/');
url = split.shift();

if (url === '' && !options.require_host) {
return true;
}

split = url.split('@');
if (split.length > 1) {
auth = split.shift();
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 2bf79a5

Please sign in to comment.