final indentation

This commit is contained in:
Anurag123Chauhan committed 2025-05-16 19:32:34 +05:30
1 parent ed03c17c22
commit fcfb225410
1 file changed
+355 -355
+355 -355
View File
@@ -4,403 +4,403 @@ import dayjs from "dayjs";
const THRESHOLD_COMMON_BASE_MSG = "Threshold must be a number."; const THRESHOLD_COMMON_BASE_MSG = "Threshold must be a number.";
const nameSchema = joi const nameSchema = joi
.string() .string()
.max(50) .max(50)
.trim() .trim()
.pattern(/^[\p{L}\p{M}''\- ]+$/u) .pattern(/^[\p{L}\p{M}''\- ]+$/u)
.messages({ .messages({
"string.empty": "Name is required", "string.empty": "Name is required",
"string.max": "Name must be less than 50 characters", "string.max": "Name must be less than 50 characters",
"string.pattern.base": "string.pattern.base":
"Name must contain only letters, spaces, apostrophes, or hyphens", "Name must contain only letters, spaces, apostrophes, or hyphens",
}); });
const lastnameSchema = joi const lastnameSchema = joi
.string() .string()
.max(50) .max(50)
.trim() .trim()
.pattern(/^[\p{L}\p{M}''\- ]+$/u) .pattern(/^[\p{L}\p{M}''\- ]+$/u)
.messages({ .messages({
"string.empty": "Surname is required", "string.empty": "Surname is required",
"string.max": "Surname must be less than 50 characters", "string.max": "Surname must be less than 50 characters",
"string.pattern.base": "Surname must contain only letters, spaces, apostrophes, or hyphens" "string.pattern.base": "Surname must contain only letters, spaces, apostrophes, or hyphens"
}); });
const passwordSchema = joi const passwordSchema = joi
.string() .string()
.trim() .trim()
.min(8) .min(8)
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/[A-Z]/.test(value)) { if (!/[A-Z]/.test(value)) {
return helpers.error("uppercase"); return helpers.error("uppercase");
} }
return value; return value;
}) })
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/[a-z]/.test(value)) { if (!/[a-z]/.test(value)) {
return helpers.error("lowercase"); return helpers.error("lowercase");
} }
return value; return value;
}) })
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/\d/.test(value)) { if (!/\d/.test(value)) {
return helpers.error("number"); return helpers.error("number");
} }
return value; return value;
}) })
.custom((value, helpers) => { .custom((value, helpers) => {
if (!/[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]/.test(value)) { if (!/[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]/.test(value)) {
return helpers.error("special"); return helpers.error("special");
} }
return value; return value;
}) })
.messages({ .messages({
"string.empty": "Password is required", "string.empty": "Password is required",
"string.min": "Password must be at least 8 characters long", "string.min": "Password must be at least 8 characters long",
uppercase: "Password must contain at least one uppercase letter", uppercase: "Password must contain at least one uppercase letter",
lowercase: "Password must contain at least one lowercase letter", lowercase: "Password must contain at least one lowercase letter",
number: "Password must contain at least one number", number: "Password must contain at least one number",
special: "Password must contain at least one special character", special: "Password must contain at least one special character",
}); });
const credentials = joi.object({ const credentials = joi.object({
firstName: nameSchema, firstName: nameSchema,
lastName: lastnameSchema, lastName: lastnameSchema,
email: joi email: joi
.string() .string()
.trim() .trim()
.email({ tlds: { allow: false } }) .email({ tlds: { allow: false } })
.custom((value, helpers) => { .custom((value, helpers) => {
const lowercasedValue = value.toLowerCase(); const lowercasedValue = value.toLowerCase();
if (value !== lowercasedValue) { if (value !== lowercasedValue) {
return helpers.message("Email must be in lowercase"); return helpers.message("Email must be in lowercase");
} }
return lowercasedValue; return lowercasedValue;
}) })
.messages({ .messages({
"string.empty": "authRegisterEmailRequired", "string.empty": "authRegisterEmailRequired",
"string.email": "authRegisterEmailInvalid", "string.email": "authRegisterEmailInvalid",
}), }),
password: passwordSchema, password: passwordSchema,
newPassword: passwordSchema, newPassword: passwordSchema,
confirm: joi confirm: joi
.string() .string()
.trim() .trim()
.custom((value, helpers) => { .custom((value, helpers) => {
const { password } = helpers.prefs.context; const { password } = helpers.prefs.context;
if (value !== password) { if (value !== password) {
return helpers.error("different"); return helpers.error("different");
} }
return value; return value;
}) })
.messages({ .messages({
"string.empty": "This field can't be empty", "string.empty": "This field can't be empty",
different: "Passwords do not match", different: "Passwords do not match",
}), }),
role: joi.array(), role: joi.array(),
teamId: joi.string().allow("").optional(), teamId: joi.string().allow("").optional(),
inviteToken: joi.string().allow(""), inviteToken: joi.string().allow(""),
}); });
const monitorValidation = joi.object({ const monitorValidation = joi.object({
url: joi url: joi
.when("type", { .when("type", {
is: "docker", is: "docker",
then: joi then: joi
.string() .string()
.trim() .trim()
.regex(/^[a-z0-9]{64}$/) .regex(/^[a-z0-9]{64}$/)
.messages({ .messages({
"string.empty": "This field is required.", "string.empty": "This field is required.",
"string.pattern.base": "Please enter a valid 64-character Docker container ID.", "string.pattern.base": "Please enter a valid 64-character Docker container ID.",
}), }),
otherwise: joi otherwise: joi
.string() .string()
.trim() .trim()
.custom((value, helpers) => { .custom((value, helpers) => {
// Regex from https://gist.github.com/dperini/729294 // Regex from https://gist.github.com/dperini/729294
var urlRegex = new RegExp( var urlRegex = new RegExp(
"^" + "^" +
// protocol identifier (optional) // protocol identifier (optional)
// short syntax // still required // short syntax // still required
"(?:(?:https?|ftp):\\/\\/)?" + "(?:(?:https?|ftp):\\/\\/)?" +
// user:pass BasicAuth (optional) // user:pass BasicAuth (optional)
"(?:" + "(?:" +
// IP address dotted notation octets // IP address dotted notation octets
// excludes loopback network 0.0.0.0 // excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0 // excludes reserved space >= 224.0.0.0
// excludes network & broadcast addresses // excludes network & broadcast addresses
// (first & last IP address of each class) // (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
"|" + "|" +
// host & domain names, may end with dot // host & domain names, may end with dot
// can be replaced by a shortest alternative // can be replaced by a shortest alternative
// (?![-_])(?:[-\\w\\u00a1-\\uffff]{0,63}[^-_]\\.)+ // (?![-_])(?:[-\\w\\u00a1-\\uffff]{0,63}[^-_]\\.)+
"(?:" + "(?:" +
"(?:" + "(?:" +
"[a-z0-9\\u00a1-\\uffff]" + "[a-z0-9\\u00a1-\\uffff]" +
"[a-z0-9\\u00a1-\\uffff_-]{0,62}" + "[a-z0-9\\u00a1-\\uffff_-]{0,62}" +
")?" + ")?" +
"[a-z0-9\\u00a1-\\uffff]\\." + "[a-z0-9\\u00a1-\\uffff]\\." +
")+" + ")+" +
// TLD identifier name, may end with dot // TLD identifier name, may end with dot
"(?:[a-z\\u00a1-\\uffff]{2,}\\.?)" + "(?:[a-z\\u00a1-\\uffff]{2,}\\.?)" +
")" + ")" +
// port number (optional) // port number (optional)
"(?::\\d{2,5})?" + "(?::\\d{2,5})?" +
// resource path (optional) // resource path (optional)
"(?:[/?#]\\S*)?" + "(?:[/?#]\\S*)?" +
"$", "$",
"i" "i"
); );
if (!urlRegex.test(value)) { if (!urlRegex.test(value)) {
return helpers.error("string.invalidUrl"); return helpers.error("string.invalidUrl");
} }
return value; return value;
}) })
.messages({ .messages({
"string.empty": "This field is required.", "string.empty": "This field is required.",
"string.uri": "The URL you provided is not valid.", "string.uri": "The URL you provided is not valid.",
"string.invalidUrl": "Please enter a valid URL with optional port", "string.invalidUrl": "Please enter a valid URL with optional port",
}), }),
}), }),
port: joi port: joi
.number() .number()
.integer() .integer()
.min(1) .min(1)
.max(65535) .max(65535)
.when("type", { .when("type", {
is: "port", is: "port",
then: joi.number().messages({ then: joi.number().messages({
"number.base": "Port must be a number.", "number.base": "Port must be a number.",
"number.min": "Port must be at least 1.", "number.min": "Port must be at least 1.",
"number.max": "Port must be at most 65535.", "number.max": "Port must be at most 65535.",
"any.required": "Port is required for port monitors.", "any.required": "Port is required for port monitors.",
}), }),
otherwise: joi.optional(), otherwise: joi.optional(),
}), }),
name: joi.string().trim().max(50).allow("").messages({ name: joi.string().trim().max(50).allow("").messages({
"string.max": "This field should not exceed the 50 characters limit.", "string.max": "This field should not exceed the 50 characters limit.",
}), }),
type: joi.string().trim().messages({ "string.empty": "This field is required." }), type: joi.string().trim().messages({ "string.empty": "This field is required." }),
ignoreTlsErrors: joi.boolean(), ignoreTlsErrors: joi.boolean(),
interval: joi.number().messages({ interval: joi.number().messages({
"number.base": "Frequency must be a number.", "number.base": "Frequency must be a number.",
"any.required": "Frequency is required.", "any.required": "Frequency is required.",
}), }),
expectedValue: joi.string().allow(""), expectedValue: joi.string().allow(""),
jsonPath: joi.string().allow(""), jsonPath: joi.string().allow(""),
matchMethod: joi.string(), matchMethod: joi.string(),
}); });
const imageValidation = joi.object({ const imageValidation = joi.object({
type: joi.string().valid("image/jpeg", "image/png").messages({ type: joi.string().valid("image/jpeg", "image/png").messages({
"any.only": "Invalid file format.", "any.only": "Invalid file format.",
"string.empty": "File type required.", "string.empty": "File type required.",
}), }),
size: joi size: joi
.number() .number()
.max(3 * 1024 * 1024) .max(3 * 1024 * 1024)
.messages({ .messages({
"number.base": "File size must be a number.", "number.base": "File size must be a number.",
"number.max": "File size must be less than 3 MB.", "number.max": "File size must be less than 3 MB.",
"number.empty": "File size required.", "number.empty": "File size required.",
}), }),
}); });
const logoImageValidation = joi const logoImageValidation = joi
.object({ .object({
src: joi.string(), src: joi.string(),
name: joi.string(), name: joi.string(),
type: joi type: joi
.string() .string()
.valid("image/jpeg", "image/png") .valid("image/jpeg", "image/png")
.allow(null) // Allow null and empty string .allow(null) // Allow null and empty string
.messages({ .messages({
"any.only": "Invalid file format.", "any.only": "Invalid file format.",
"string.empty": "File type required.", "string.empty": "File type required.",
}) })
.optional(), .optional(),
size: joi size: joi
.number() .number()
.max(3000000) .max(3000000)
.allow(null) // Allow null and empty string .allow(null) // Allow null and empty string
.messages({ .messages({
"number.base": "File size must be a number.", "number.base": "File size must be a number.",
"number.max": "File size must be less than 3MB.", "number.max": "File size must be less than 3MB.",
"number.empty": "File size required.", "number.empty": "File size required.",
}) })
.optional(), .optional(),
}) })
.allow(null) .allow(null)
.optional(); // Make entire object optional .optional(); // Make entire object optional
const statusPageValidation = joi.object({ const statusPageValidation = joi.object({
type: joi.string().valid("uptime", "distributed").required(), type: joi.string().valid("uptime", "distributed").required(),
isPublished: joi.bool(), isPublished: joi.bool(),
companyName: joi companyName: joi
.string() .string()
.trim() .trim()
.messages({ "string.empty": "Company name is required." }), .messages({ "string.empty": "Company name is required." }),
url: joi url: joi
.string() .string()
.pattern(/^[a-zA-Z0-9_-]+$/) // Only allow alphanumeric, underscore, and hyphen .pattern(/^[a-zA-Z0-9_-]+$/) // Only allow alphanumeric, underscore, and hyphen
.required() .required()
.messages({ .messages({
"string.pattern.base": "string.pattern.base":
"URL can only contain letters, numbers, underscores, and hyphens", "URL can only contain letters, numbers, underscores, and hyphens",
}), }),
timezone: joi.string().trim().messages({ "string.empty": "Timezone is required." }), timezone: joi.string().trim().messages({ "string.empty": "Timezone is required." }),
color: joi.string().trim().messages({ "string.empty": "Color is required." }), color: joi.string().trim().messages({ "string.empty": "Color is required." }),
theme: joi.string(), theme: joi.string(),
monitors: joi.array().min(1).required().messages({ monitors: joi.array().min(1).required().messages({
"string.pattern.base": "Must be a valid monitor ID", "string.pattern.base": "Must be a valid monitor ID",
"array.base": "Monitors must be an array", "array.base": "Monitors must be an array",
"array.min": "At least one monitor is required", "array.min": "At least one monitor is required",
"array.empty": "At least one monitor is required", "array.empty": "At least one monitor is required",
"any.required": "At least one monitor is required", "any.required": "At least one monitor is required",
}), }),
subMonitors: joi.array().optional(), subMonitors: joi.array().optional(),
logo: logoImageValidation, logo: logoImageValidation,
showUptimePercentage: joi.boolean(), showUptimePercentage: joi.boolean(),
showCharts: joi.boolean(), showCharts: joi.boolean(),
}); });
const settingsValidation = joi.object({ const settingsValidation = joi.object({
checkTTL: joi.number().required().messages({ checkTTL: joi.number().required().messages({
"string.empty": "Please enter a value", "string.empty": "Please enter a value",
"number.base": "Please enter a valid number", "number.base": "Please enter a valid number",
"any.required": "Please enter a value", "any.required": "Please enter a value",
}), }),
pagespeedApiKey: joi.string().allow("").optional(), pagespeedApiKey: joi.string().allow("").optional(),
language: joi.string().required(), language: joi.string().required(),
systemEmailHost: joi.string().allow(""), systemEmailHost: joi.string().allow(""),
systemEmailPort: joi.number().allow(null, ""), systemEmailPort: joi.number().allow(null, ""),
systemEmailAddress: joi.string().allow(""), systemEmailAddress: joi.string().allow(""),
systemEmailPassword: joi.string().allow(""), systemEmailPassword: joi.string().allow(""),
systemEmailUser: joi.string().allow(""), systemEmailUser: joi.string().allow(""),
}); });
const dayjsValidator = (value, helpers) => { const dayjsValidator = (value, helpers) => {
if (!dayjs(value).isValid()) { if (!dayjs(value).isValid()) {
return helpers.error("any.invalid"); return helpers.error("any.invalid");
} }
return value; return value;
}; };
const maintenanceWindowValidation = joi.object({ const maintenanceWindowValidation = joi.object({
repeat: joi.string(), repeat: joi.string(),
startDate: joi.custom(dayjsValidator, "Day.js date validation"), startDate: joi.custom(dayjsValidator, "Day.js date validation"),
startTime: joi.custom(dayjsValidator, "Day.js date validation"), startTime: joi.custom(dayjsValidator, "Day.js date validation"),
duration: joi.number().integer().min(0), duration: joi.number().integer().min(0),
durationUnit: joi.string(), durationUnit: joi.string(),
name: joi.string(), name: joi.string(),
monitors: joi.array().min(1), monitors: joi.array().min(1),
}); });
const advancedSettingsValidation = joi.object({ const advancedSettingsValidation = joi.object({
apiBaseUrl: joi.string().uri({ allowRelative: true }).trim().messages({ apiBaseUrl: joi.string().uri({ allowRelative: true }).trim().messages({
"string.empty": "API base url is required.", "string.empty": "API base url is required.",
"string.uri": "The URL you provided is not valid.", "string.uri": "The URL you provided is not valid.",
}), }),
logLevel: joi.string().valid("debug", "none", "error", "warn").allow(""), logLevel: joi.string().valid("debug", "none", "error", "warn").allow(""),
systemEmailHost: joi.string().allow(""), systemEmailHost: joi.string().allow(""),
systemEmailPort: joi.number().allow(null, ""), systemEmailPort: joi.number().allow(null, ""),
systemEmailAddress: joi.string().allow(""), systemEmailAddress: joi.string().allow(""),
systemEmailPassword: joi.string().allow(""), systemEmailPassword: joi.string().allow(""),
jwtTTLNum: joi.number().messages({ jwtTTLNum: joi.number().messages({
"number.base": "JWT TTL is required.", "number.base": "JWT TTL is required.",
}), }),
jwtTTLUnits: joi jwtTTLUnits: joi
.string() .string()
.trim() .trim()
.custom((value, helpers) => { .custom((value, helpers) => {
if (!["days", "hours"].includes(value)) { if (!["days", "hours"].includes(value)) {
return helpers.message("JWT TTL unit is required."); return helpers.message("JWT TTL unit is required.");
} }
return value; return value;
}), }),
dbType: joi.string().trim().messages({ dbType: joi.string().trim().messages({
"string.empty": "DB type is required.", "string.empty": "DB type is required.",
}), }),
redisHost: joi.string().trim().messages({ redisHost: joi.string().trim().messages({
"string.empty": "Redis host is required.", "string.empty": "Redis host is required.",
}), }),
redisPort: joi.number().allow(null, ""), redisPort: joi.number().allow(null, ""),
pagespeedApiKey: joi.string().allow(""), pagespeedApiKey: joi.string().allow(""),
}); });
const infrastructureMonitorValidation = joi.object({ const infrastructureMonitorValidation = joi.object({
url: joi url: joi
.string() .string()
.trim() .trim()
.custom((value, helpers) => { .custom((value, helpers) => {
const urlRegex = const urlRegex =
/^(https?:\/\/)?(([0-9]{1,3}\.){3}[0-9]{1,3}|[\da-z\.-]+)(\.[a-z\.]{2,6})?(:(\d+))?([\/\w \.-]*)*\/?$/i; /^(https?:\/\/)?(([0-9]{1,3}\.){3}[0-9]{1,3}|[\da-z\.-]+)(\.[a-z\.]{2,6})?(:(\d+))?([\/\w \.-]*)*\/?$/i;
if (!urlRegex.test(value)) { if (!urlRegex.test(value)) {
return helpers.error("string.invalidUrl"); return helpers.error("string.invalidUrl");
} }
return value; return value;
}) })
.messages({ .messages({
"string.empty": "This field is required.", "string.empty": "This field is required.",
"string.uri": "The URL you provided is not valid.", "string.uri": "The URL you provided is not valid.",
"string.invalidUrl": "Please enter a valid URL with optional port", "string.invalidUrl": "Please enter a valid URL with optional port",
}), }),
name: joi.string().trim().max(50).allow("").messages({ name: joi.string().trim().max(50).allow("").messages({
"string.max": "This field should not exceed the 50 characters limit.", "string.max": "This field should not exceed the 50 characters limit.",
}), }),
secret: joi.string().trim().messages({ "string.empty": "This field is required." }), secret: joi.string().trim().messages({ "string.empty": "This field is required." }),
usage_cpu: joi.number().messages({ usage_cpu: joi.number().messages({
"number.base": THRESHOLD_COMMON_BASE_MSG, "number.base": THRESHOLD_COMMON_BASE_MSG,
}), }),
cpu: joi.boolean(), cpu: joi.boolean(),
memory: joi.boolean(), memory: joi.boolean(),
disk: joi.boolean(), disk: joi.boolean(),
temperature: joi.boolean(), temperature: joi.boolean(),
usage_memory: joi.number().messages({ usage_memory: joi.number().messages({
"number.base": THRESHOLD_COMMON_BASE_MSG, "number.base": THRESHOLD_COMMON_BASE_MSG,
}), }),
usage_disk: joi.number().messages({ usage_disk: joi.number().messages({
"number.base": THRESHOLD_COMMON_BASE_MSG, "number.base": THRESHOLD_COMMON_BASE_MSG,
}), }),
usage_temperature: joi.number().messages({ usage_temperature: joi.number().messages({
"number.base": "Temperature must be a number.", "number.base": "Temperature must be a number.",
}), }),
// usage_system: joi.number().messages({ // usage_system: joi.number().messages({
// "number.base": "System load must be a number.", // "number.base": "System load must be a number.",
// }), // }),
// usage_swap: joi.number().messages({ // usage_swap: joi.number().messages({
// "number.base": "Swap used must be a number.", // "number.base": "Swap used must be a number.",
// }), // }),
interval: joi.number().messages({ interval: joi.number().messages({
"number.base": "Frequency must be a number.", "number.base": "Frequency must be a number.",
"any.required": "Frequency is required.", "any.required": "Frequency is required.",
}), }),
notifications: joi.array().items( notifications: joi.array().items(
joi.object({ joi.object({
type: joi.string().valid("email").required(), type: joi.string().valid("email").required(),
address: joi address: joi
.string() .string()
.email({ tlds: { allow: false } }) .email({ tlds: { allow: false } })
.required(), .required(),
}) })
), ),
}); });
export { export {
credentials, credentials,
imageValidation, imageValidation,
monitorValidation, monitorValidation,
settingsValidation, settingsValidation,
maintenanceWindowValidation, maintenanceWindowValidation,
advancedSettingsValidation, advancedSettingsValidation,
infrastructureMonitorValidation, infrastructureMonitorValidation,
statusPageValidation, statusPageValidation,
logoImageValidation, logoImageValidation,
}; };