Browse Source

Explicitly validate slashes presence

master
Sven Slootweg 1 year ago
parent
commit
8fdab33e8b
  1. 11
      example.js
  2. 6
      index.js

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

6
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) {

Loading…
Cancel
Save