You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
925 B
JavaScript

"use strict";
const isURL = require("@validatem/is-url");
const wrapError = require("@validatem/wrap-error");
const ignoreResult = require("@validatem/ignore-result");
const ValidationError = require("@validatem/error");
module.exports = function (options = {}) {
let allowedProtocols = (options.permitInsecure === true)
? [ "http://", "https://" ]
: [ "https://" ];
return wrapError("modular-matrix.is-homeserver-url", "Must be a valid Matrix homeserver URL", ignoreResult([
isURL(allowedProtocols),
function isHomeserverURL(value) {
if (value.pathname != null && value.pathname !== "/") {
throw new ValidationError(`Must not include a path`, { code: "modular-matrix.is-homeserver-url.path" });
} else if (value.query != null) {
throw new ValidationError(`Must not include query parameters`, { code: "modular-matrix.is-homeserver-url.query" });
}
}
]), { preserveOriginalErrors: true });
};