Skip to content

Commit 135081f

Browse files
committed
Accept constraints in URI assert without hostnames or protocols
1 parent 6116f71 commit 135081f

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/asserts/uri-assert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
const _ = require('lodash');
78
const { Validator, Violation } = require('validator.js');
89
const { forEach, has } = require('lodash');
910

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

5455
// URIs must have at least a hostname and protocol.
55-
if (!uri.hostname() || !uri.protocol()) {
56+
if (_.isEmpty(this.constraints) && (!uri.hostname() || !uri.protocol())) {
5657
throw new Violation(this, value, { constraints: this.constraints });
5758
}
5859

test/asserts/uri-assert.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ describe('UriAssert', () => {
106106
}
107107
});
108108

109+
it('should accept an `is` constraint without a hostname or protocol', () => {
110+
Assert.uri({ is: 'relative' }).validate('/dashboard');
111+
});
112+
109113
it('should accept an uri that matches the constraints', () => {
110114
Assert.uri({ is: 'domain' }).validate('https://foobar.com');
111115
Assert.uri({ protocol: 'https' }).validate('https://foobar.com');

0 commit comments

Comments
 (0)