diff --git a/lib/types/string.js b/lib/types/string.js index 8496b23f0..4f3dc7f1a 100755 --- a/lib/types/string.js +++ b/lib/types/string.js @@ -642,10 +642,11 @@ module.exports = Any.extend({ const match = regex.exec(value); if (match) { - if (domain && - !Domain.isValid(match[1], domain)) { - - return helpers.error('string.domain', { value: match[1] }); + if (domain) { + const matched = match[1] || match[2]; + if (!Domain.isValid(matched, domain)) { + return helpers.error('string.domain', { value: matched }); + } } return value; diff --git a/package.json b/package.json index df971f13c..3a6618343 100755 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "validation" ], "dependencies": { - "@hapi/address": "^4.0.0", + "@hapi/address": "^4.0.1", "@hapi/formula": "^2.0.0", "@hapi/hoek": "^9.0.0", "@hapi/pinpoint": "^2.0.0", diff --git a/test/types/string.js b/test/types/string.js index 8c16679c7..0c932d230 100755 --- a/test/types/string.js +++ b/test/types/string.js @@ -8464,6 +8464,25 @@ describe('string', () => { ]); }); + it('validates relative only uri with domain', () => { + + const schema = Joi.string().uri({ relativeOnly: true, domain: { minDomainSegments: 3, tlds: { allow: ['com'] } } }); + Helper.validate(schema, [ + ['foo://example.com:8042/over/there?name=ferret#nose', false, '"value" must be a valid relative uri'], + ['//test.example.com:8042/over/there?name=ferret#nose', true], + ['//example.com:8042/over/there?name=ferret#nose', false, '"value" must contain a valid domain name'] + ]); + }); + + it('validates allowed relative uri with domain', () => { + + const schema = Joi.string().uri({ allowRelative: true, domain: { minDomainSegments: 3, tlds: { allow: ['com'] } } }); + Helper.validate(schema, [ + ['//test.example.com:8042/over/there?name=ferret#nose', true], + ['//example.com:8042/over/there?name=ferret#nose', false, '"value" must contain a valid domain name'] + ]); + }); + it('validates uri with square brackets allowed', () => { const schema = Joi.string().uri({ allowQuerySquareBrackets: true });