diff --git a/src/lib/isURL.js b/src/lib/isURL.js index aa6222a90..8ed3f0728 100644 --- a/src/lib/isURL.js +++ b/src/lib/isURL.js @@ -152,6 +152,11 @@ export default function isURL(url, options) { if (options.host_whitelist) { return checkHost(host, options.host_whitelist); } + + if (host === '' && !options.require_host) { + return true; + } + if (!isIP(host) && !isFQDN(host, options) && (!ipv6 || !isIP(ipv6, 6))) { return false; } diff --git a/test/validators.js b/test/validators.js index 50c646f18..b3a39bfdf 100644 --- a/test/validators.js +++ b/test/validators.js @@ -472,6 +472,24 @@ describe('Validators', () => { }); }); + it('should validate postgres URLs without a host', () => { + test({ + validator: 'isURL', + args: [{ + protocols: ['postgres'], + require_host: false, + }], + valid: [ + 'postgres://user:pw@/test', + ], + invalid: [ + 'http://foobar.com', + 'postgres://', + ], + }); + }); + + it('should validate URLs with any protocol', () => { test({ validator: 'isURL',