Compare commits

...

3 Commits

@ -10,9 +10,14 @@ console.log(validateValue("bar", [ isURL() ])); /*
- At (root): Must be a valid URL - 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: 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) { function isURL(value) {
let parsed = url.parse(value); let parsed = url.parse(value);
let slashes = (parsed.slashes === true)
? "//"
: "";
let parsedProtocol = (parsed.protocol != null) let parsedProtocol = (parsed.protocol != null)
? parsed.protocol.replace(/:$/, "") ? parsed.protocol + slashes
: null; : null;
if (parsedProtocol == 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())) { } else if (protocolSet != null && !protocolSet.has(parsedProtocol.toLowerCase())) {
let validProtocolList = protocols let validProtocolList = protocols
.map((protocol) => protocol.toUpperCase()) .map((protocol) => protocol.toUpperCase())
.join(", "); .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 { } else {
return parsed; return parsed;
} }

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

Loading…
Cancel
Save