Browse Source

Ensure that we're always dealing with a string

master
Sven Slootweg 1 year ago
parent
commit
b845035d45
  1. 34
      index.js
  2. 3
      package.json
  3. 13
      yarn.lock

34
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;
}
}
}
];
};

3
package.json

@ -11,7 +11,8 @@
"author": "Sven Slootweg <admin@cryto.net>",
"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"

13
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"

Loading…
Cancel
Save