Files
Checkmate/Client/src/Validation/validation.js
T

161 lines
4.6 KiB
JavaScript
Raw Normal View History

import joi from "joi";
2024-07-15 16:32:24 -04:00
const nameSchema = joi
.string()
.max(50)
.trim()
2024-07-18 20:31:47 -04:00
.pattern(/^[A-Za-z]+$/)
2024-07-15 16:32:24 -04:00
.messages({
"string.empty": "Name is required",
"string.max": "Name must be less than 50 characters long",
"string.pattern.base": "Name must contain only letters",
});
const passwordSchema = joi
.string()
.trim()
.min(8)
.messages({
"string.empty": "Password is required",
"string.min": "Password must be at least 8 characters long",
})
.custom((value, helpers) => {
if (!/[A-Z]/.test(value)) {
return helpers.message(
"Password must contain at least one uppercase letter"
);
}
if (!/[a-z]/.test(value)) {
return helpers.message(
"Password must contain at least one lowercase letter"
);
}
if (!/\d/.test(value)) {
return helpers.message("Password must contain at least one number");
}
if (!/[!@#$%^&*]/.test(value)) {
return helpers.message(
"Password must contain at least one special character"
);
}
return value;
});
const credentials = joi.object({
2024-07-22 12:40:44 -04:00
firstName: nameSchema,
lastName: nameSchema,
2024-07-15 16:32:24 -04:00
email: joi
.string()
.trim()
.email({ tlds: { allow: false } })
2024-08-23 10:30:27 -07:00
.custom((value, helpers) => {
const lowercasedValue = value.toLowerCase();
if (value !== lowercasedValue) {
return helpers.message("Email must be in lowercase");
}
return lowercasedValue;
})
2024-07-15 16:32:24 -04:00
.messages({
"string.empty": "Email is required",
"string.email": "Must be a valid email address",
}),
password: passwordSchema,
newPassword: passwordSchema,
confirm: joi
.string()
.trim()
.messages({
"string.empty": "Password confirmation is required",
})
.custom((value, helpers) => {
2024-07-15 18:47:23 -04:00
const { password } = helpers.prefs.context;
if (value !== password) {
2024-07-15 16:32:24 -04:00
return helpers.message("Passwords do not match");
}
return value;
}),
role: joi.array(),
2024-08-26 13:29:11 -07:00
teamId: joi.string().allow("").optional(),
2024-07-15 16:32:24 -04:00
});
2024-07-19 18:04:58 -04:00
const monitorValidation = joi.object({
2024-08-23 10:30:27 -07:00
url: joi.string().uri({ allowRelative: true }).trim().messages({
"string.empty": "This field is required.",
"string.uri": "The URL you provided is not valid.",
}),
name: joi.string().trim().max(50).allow("").messages({
"string.max": "This field should not exceed the 50 characters limit.",
}),
type: joi
.string()
.trim()
.messages({ "string.empty": "This field is required." }),
2024-07-19 18:04:58 -04:00
interval: joi.number().messages({
"number.base": "Frequency must be a number.",
"any.required": "Frequency is required.",
}),
2024-06-27 17:26:00 -04:00
});
2024-07-02 14:51:31 -04:00
const imageValidation = joi.object({
type: joi.string().valid("image/jpeg", "image/png").messages({
"any.only": "Invalid file format.",
"string.empty": "File type required.",
}),
size: joi
.number()
.max(3 * 1024 * 1024)
.messages({
"number.base": "File size must be a number.",
"number.max": "File size must be less than 3 MB.",
"number.empty": "File size required.",
}),
});
const maintenanceWindowValidation = joi.object({
repeat: joi.number().valid(1, 2, 3).required().messages({
"number.base": "Repeat must be a number.",
"any.only": "Repeat must be one of [1, 2, 3].",
"any.required": "Repeat is required.",
}),
date: joi.date().required().messages({
"date.base": "Date must be a valid date.",
"any.required": "Date is required.",
}),
startTime: joi.string().required().messages({
"string.base": "Start time must be a valid time.",
"any.required": "Start time is required.",
}),
duration: joi.number().required().messages({
2024-08-21 22:50:42 +03:30
"number.empty": "duration is required.",
"number.base": "Duration must be a number.",
"any.required": "Duration is required.",
}),
unit: joi.string().valid("minutes", "hours", "days").required().messages({
"string.base": "Unit must be a string.",
"any.only": "Unit must be one of ['minutes', 'hours', 'days'].",
"any.required": "Unit is required.",
}),
displayName: joi.string().max(50).required().messages({
"string.empty": "Display name is required.",
"string.max": "Display name must be less than 50 characters long",
}),
2024-08-21 22:50:42 +03:30
addMonitors: joi.string().max(50).required().messages({
"string.empty": "Add monitors is required.",
"string.max": "Add monitors must be less than 50 characters long",
}),
});
2024-09-16 12:13:29 +08:00
const settingsValidation = joi.object({
ttl: joi.number().required().messages({
"string.empty": "TTL is required",
}),
});
export {
credentials,
imageValidation,
monitorValidation,
maintenanceWindowValidation,
2024-09-16 12:13:29 +08:00
settingsValidation,
};