Compare commits

...

3 Commits

@ -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
*/

@ -18,18 +18,22 @@ 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) {
return new ValidationError("Must be a valid URL");
return new ValidationError("Must be a valid URL", { code: "validatem.is-url.invalid-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`);
return new ValidationError(`Must be a URL with one of the following protocols: ${validProtocolList} - but got ${parsedProtocol.toUpperCase()} instead`, { code: "validatem.is-url.wrong-protocol" });
} else {
return parsed;
}

@ -5,7 +5,7 @@
"validatem",
"validator"
],
"version": "0.1.1",
"version": "0.2.0",
"main": "index.js",
"repository": "http://git.cryto.net/validatem/is-url.git",
"author": "Sven Slootweg <admin@cryto.net>",

Loading…
Cancel
Save