From 135081f4a8da00d77de00acb96ae8bc3468ff833 Mon Sep 17 00:00:00 2001 From: Nuno Sousa Date: Thu, 28 May 2020 14:29:06 +0100 Subject: [PATCH] Accept constraints in URI assert without hostnames or protocols --- src/asserts/uri-assert.js | 3 ++- test/asserts/uri-assert.test.js | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/asserts/uri-assert.js b/src/asserts/uri-assert.js index 89da111..4260c23 100644 --- a/src/asserts/uri-assert.js +++ b/src/asserts/uri-assert.js @@ -4,6 +4,7 @@ * Module dependencies. */ +const _ = require('lodash'); const { Validator, Violation } = require('validator.js'); const { forEach, has } = require('lodash'); @@ -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 }); } diff --git a/test/asserts/uri-assert.test.js b/test/asserts/uri-assert.test.js index d4af206..94df01f 100644 --- a/test/asserts/uri-assert.test.js +++ b/test/asserts/uri-assert.test.js @@ -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');