From 8fdab33e8bc40e83f44f6b12c81cf98fd9ad0226 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Thu, 6 Aug 2020 12:43:14 +0200 Subject: [PATCH] Explicitly validate slashes presence --- example.js | 11 ++++++++--- index.js | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/example.js b/example.js index 2e63309..77ff335 100644 --- a/example.js +++ b/example.js @@ -10,9 +10,14 @@ console.log(validateValue("bar", [ isURL() ])); /* - At (root): Must be a valid URL */ -console.log(validateValue("https://example.com/", [ isURL([ "http", "https" ]) ])); // Url { ... } +console.log(validateValue("https://example.com/", [ isURL([ "http://", "https://" ]) ])); // Url { ... } -console.log(validateValue("ftp://example.com/", [ isURL([ "http", "https" ]) ])); /* +console.log(validateValue("https:example.com/", [ isURL([ "http://", "https://" ]) ])); /* AggregrateValidationError: One or more validation errors occurred: - - At (root): Must be a URL with one of the following protocols: HTTP, HTTPS - but got FTP instead + - At (root): Must be a URL with one of the following protocols: HTTP://, HTTPS:// - but got HTTPS: instead +*/ + +console.log(validateValue("ftp://example.com/", [ isURL([ "http://", "https://" ]) ])); /* + AggregrateValidationError: One or more validation errors occurred: + - At (root): Must be a URL with one of the following protocols: HTTP://, HTTPS:// - but got FTP:// instead */ diff --git a/index.js b/index.js index 4a21db2..175678f 100644 --- a/index.js +++ b/index.js @@ -18,8 +18,12 @@ module.exports = function (protocols) { function isURL(value) { let parsed = url.parse(value); + let slashes = (parsed.slashes === true) + ? "//" + : ""; + let parsedProtocol = (parsed.protocol != null) - ? parsed.protocol.replace(/:$/, "") + ? parsed.protocol + slashes : null; if (parsedProtocol == null) {