Skip to content

Commit

Permalink
Accept constraints in URI assert without hostnames or protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofgs committed May 29, 2020
1 parent 6116f71 commit 135081f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/asserts/uri-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/

const _ = require('lodash');
const { Validator, Violation } = require('validator.js');
const { forEach, has } = require('lodash');

Expand Down Expand Up @@ -52,7 +53,7 @@ module.exports = function uriAssert(constraints) {
const uri = new URI(value);

// URIs must have at least a hostname and protocol.
if (!uri.hostname() || !uri.protocol()) {
if (_.isEmpty(this.constraints) && (!uri.hostname() || !uri.protocol())) {
throw new Violation(this, value, { constraints: this.constraints });
}

Expand Down
4 changes: 4 additions & 0 deletions test/asserts/uri-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ describe('UriAssert', () => {
}
});

it('should accept an `is` constraint without a hostname or protocol', () => {
Assert.uri({ is: 'relative' }).validate('/dashboard');
});

it('should accept an uri that matches the constraints', () => {
Assert.uri({ is: 'domain' }).validate('https://foobar.com');
Assert.uri({ protocol: 'https' }).validate('https://foobar.com');
Expand Down

0 comments on commit 135081f

Please sign in to comment.