This commit is contained in:
Kim Ravn Hansen
2025-09-14 13:04:48 +02:00
parent 8c196bb6a1
commit ed91a7f2f7
43 changed files with 2279 additions and 1727 deletions

44
server/utils/mustbe.js Executable file
View File

@@ -0,0 +1,44 @@
export function mustBe(value, ...types) {
//
// empty type enforcement.
// Means we just want value to be define
if (types.length === 0 && typeof value !== "undefined") {
return value;
}
//
// value has a valid type
if (types.includes(typeof value)) {
return value;
}
// NOTE: only checks first element of array if it's a string.
if (types.includes("strings[]") && Array.isArray(value) && (value.length === 0 || typeof value[0] === "string")) {
return value;
}
throw new Error("Invalid data type. Expected >>" + types.join(" or ") + "<< but got " + typeof value);
}
export function mustBeString(value) {
return mustBe(value, "string");
}
export function mustBeInteger(value) {
if (typeof value === "number" && Number.isSafeInteger(value)) {
return value;
}
}
/**
*
* @param {string} str
* @param {RegExp} regex
*/
export function mustMatch(str, regex) {
if (!regex.test(str)) {
throw new Error(`String did not satisfy ${regex}`);
}
return str;
}