adding validations and toasts as validation error

This commit is contained in:
Mohammad Khalilzadeh committed 2024-08-21 09:40:09 +03:30
1 parent 0ad08f431e
commit b247fabde0
2 files changed
+66 -19

No files matched your search

@@ -10,7 +10,9 @@ import dayjs from "dayjs";
import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker"; import { MobileTimePicker } from "@mui/x-date-pickers/MobileTimePicker";
import Field from "../../../Components/Inputs/Field"; import Field from "../../../Components/Inputs/Field";
import axios from "axios"; import { maintenanceWindowValidation } from "../../../Validation/validation";
import { logger } from "../../../Utils/Logger";
import { createToast } from "../../../Utils/toastUtils";
const directory = { const directory = {
title: "Create a maintenance window", title: "Create a maintenance window",
@@ -63,6 +65,7 @@ const CreateNewMaintenanceWindow = () => {
friendlyName: "", friendlyName: "",
AddMonitors: "", AddMonitors: "",
}); });
const [errors, setErrors] = useState({});
const handleChange = (event, name) => { const handleChange = (event, name) => {
const { value } = event.target; const { value } = event.target;
@@ -83,16 +86,26 @@ const CreateNewMaintenanceWindow = () => {
addMonitors: values.AddMonitors, addMonitors: values.AddMonitors,
}; };
console.log("Submitting data: ", data); const { error } = maintenanceWindowValidation.validate(data, {
abortEarly: false,
await axios });
.post("/address-of-the-route", data) logger.log("error: ", error);
.then((res) => { if (error) {
console.log("Response: ", res.data); const newErrors = {};
}) error.details.forEach((err) => {
.catch((err) => { newErrors[err.path[0]] = err.message;
console.log("Error: ", err);
}); });
setErrors(newErrors);
createToast({
body:
error.details && error.details.length > 0
? error.details[0].message
: "Error validating data",
});
logger.error("Validation errors:", error.details);
}
logger.log("Submitting data: ", data);
}; };
const configOptions = [ const configOptions = [
+43 -9
View File
@@ -72,14 +72,10 @@ const credentials = joi.object({
}); });
const monitorValidation = joi.object({ const monitorValidation = joi.object({
url: joi url: joi.string().uri({ allowRelative: true }).trim().messages({
.string() "string.empty": "This field is required.",
.uri({ allowRelative: true }) "string.uri": "The URL you provided is not valid.",
.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({ 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.",
}), }),
@@ -108,4 +104,42 @@ const imageValidation = joi.object({
}), }),
}); });
export { credentials, imageValidation, monitorValidation }; 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({
"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.",
}),
friendlyName: joi.string().required().max(100).messages({
"string.max": "Friendly name must be less than 100 characters.",
"any.required": "Friendly name is required.",
}),
addMonitors: joi.string().required().messages({
"string.base": "Add monitors must be a string.",
"any.required": "Add monitors is required.",
}),
});
export {
credentials,
imageValidation,
monitorValidation,
maintenanceWindowValidation,
};