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