logitar-validation
Version:
JavaScript validation library distributed by Logitar.
62 lines (61 loc) • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const logitar_js_1 = require("logitar-js");
const { trimEnd } = logitar_js_1.stringUtils;
/**
* Format a protocol string to be used in a set.
* @param protocol The protocol to format.
* @returns The formatted protocol.
*/
function format(protocol) {
return trimEnd(protocol.trim().toLowerCase(), ":");
}
/**
* A validation rule that checks if a string is a valid URL.
* @param value The value to validate.
* @param args The allowed protocols.
* @returns The result of the validation rule execution.
*/
const url = (value, args) => {
if (typeof value !== "string") {
return { severity: "error", message: "{{name}} must be a string." };
}
if (value.length > 0) {
let isArgsValid = true;
const protocols = new Set(["http", "https"]);
if (typeof args !== "undefined" && typeof args !== "boolean") {
let values = [];
if (typeof args === "string") {
values = args.split(/[,;\|]/);
}
else if (Array.isArray(args)) {
values = args;
}
if (values.length === 0) {
isArgsValid = false;
}
else {
protocols.clear();
values.forEach((value) => protocols.add(format(value)));
}
}
let url;
try {
url = new URL(value.trim());
}
catch (_) {
return { severity: "error", message: "{{name}} must be a valid URL." };
}
if (!protocols.has(format(url.protocol))) {
return { severity: "error", message: `{{name}} must be an URL with one of the following protocols: ${[...protocols].join(", ")}.` };
}
if (!isArgsValid) {
return {
severity: "warning",
message: "The arguments must be undefined, a string containing the allowed protocols separated by commas, semicolons or pipes, or an array of allowed protocols.",
};
}
}
return { severity: "information" };
};
exports.default = url;