From b845035d451c305304e2acce0c45273b9ae649ff Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 3 Aug 2020 11:13:17 +0200 Subject: [PATCH] Ensure that we're always dealing with a string --- index.js | 34 +++++++++++++++++++--------------- package.json | 3 ++- yarn.lock | 13 +++++++++++++ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 8f391f3..4a21db2 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const url = require("url"); const ValidationError = require("@validatem/error"); +const isString = require("@validatem/is-string"); module.exports = function (protocols) { if (protocols != null && !Array.isArray(protocols)) { @@ -12,23 +13,26 @@ module.exports = function (protocols) { ? new Set(protocols.map((protocol) => protocol.toLowerCase())) : null; - return function isURL(value) { - let parsed = url.parse(value); + return [ + isString, + function isURL(value) { + let parsed = url.parse(value); - let parsedProtocol = (parsed.protocol != null) - ? parsed.protocol.replace(/:$/, "") - : null; + let parsedProtocol = (parsed.protocol != null) + ? parsed.protocol.replace(/:$/, "") + : null; - if (parsedProtocol == null) { - return new ValidationError("Must be a valid URL"); - } else if (protocolSet != null && !protocolSet.has(parsedProtocol.toLowerCase())) { - let validProtocolList = protocols - .map((protocol) => protocol.toUpperCase()) - .join(", "); + if (parsedProtocol == null) { + return new ValidationError("Must be a valid URL"); + } else if (protocolSet != null && !protocolSet.has(parsedProtocol.toLowerCase())) { + let validProtocolList = protocols + .map((protocol) => protocol.toUpperCase()) + .join(", "); - return new ValidationError(`Must be a URL with one of the following protocols: ${validProtocolList} - but got ${parsedProtocol.toUpperCase()} instead`); - } else { - return parsed; + return new ValidationError(`Must be a URL with one of the following protocols: ${validProtocolList} - but got ${parsedProtocol.toUpperCase()} instead`); + } else { + return parsed; + } } - } + ]; }; diff --git a/package.json b/package.json index a678420..79e1df8 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "author": "Sven Slootweg ", "license": "WTFPL OR CC0-1.0", "dependencies": { - "@validatem/error": "^1.0.0" + "@validatem/error": "^1.0.0", + "@validatem/is-string": "^0.1.1" }, "devDependencies": { "@validatem/core": "^0.3.1" diff --git a/yarn.lock b/yarn.lock index 7858c67..5ad76ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -80,6 +80,14 @@ "@validatem/error" "^1.0.0" is-plain-obj "^2.1.0" +"@validatem/is-string@^0.1.1": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@validatem/is-string/-/is-string-0.1.1.tgz#0710d8cebedd4d6861b4a8c63d7803ed6d2f9d6c" + integrity sha512-iyRVYRPgRt2ZlWyc7pzN1WkO6apzE8at39XQa4WUr8qRPfJn12V4khS9MumWbZs8N2qqajrxMigB2LJUCKOCRg== + dependencies: + "@validatem/error" "^1.0.0" + is-string "^1.0.5" + "@validatem/match-special@^0.1.0": version "0.1.0" resolved "https://registry.yarnpkg.com/@validatem/match-special/-/match-special-0.1.0.tgz#4e0c28f1aee5bf53c1ef30bbf8c755d4946ae0ff" @@ -208,6 +216,11 @@ is-regexp@^2.0.0: resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-2.1.0.tgz#cd734a56864e23b956bf4e7c66c396a4c0b22c2d" integrity sha512-OZ4IlER3zmRIoB9AqNhEggVxqIH4ofDns5nRrPS6yQxXE1TPCUpFznBfRQmQa8uC+pXqjMnukiJBxCisIxiLGA== +is-string@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.5.tgz#40493ed198ef3ff477b8c7f92f644ec82a5cd3a6" + integrity sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ== + supports-color@^7.1.0: version "7.1.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"