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

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-10-18 11:20:20 -04:00
const buildErrors = (prev, id, error) => {
const updatedErrors = { ...prev };
if (error) {
2024-11-12 10:19:03 -05:00
updatedErrors[id] = error.details[0].message ?? "Validation error";
2024-10-18 11:20:20 -04:00
} else {
delete updatedErrors[id];
}
return updatedErrors;
};
const hasValidationErrors = (form, validation, setErrors) => {
const { error } = validation.validate(form, {
abortEarly: false,
});
if (error) {
const newErrors = {};
error.details.forEach((err) => {
2024-10-24 11:31:10 -04:00
if (
![
"clientHost",
"refreshTokenSecret",
"dbConnectionString",
"refreshTokenTTL",
"jwtTTL",
2024-11-11 11:16:06 -05:00
"notify-email-list",
2024-10-24 11:31:10 -04:00
].includes(err.path[0])
) {
newErrors[err.path[0]] = err.message ?? "Validation error";
}
// Handle conditionally usage number required cases
if (!form.cpu || (form.cpu && form.usage_cpu)) {
delete newErrors["usage_cpu"];
2024-11-12 10:19:03 -05:00
}
2024-11-11 11:16:06 -05:00
if (!form.memory || (form.memory && form.usage_memory)) {
delete newErrors["usage_memory"];
2024-11-12 10:19:03 -05:00
}
2024-11-11 11:16:06 -05:00
if (!form.disk || (form.disk && form.usage_disk)) {
delete newErrors["usage_disk"];
2024-11-12 10:19:03 -05:00
}
2024-10-18 11:20:20 -04:00
});
2024-10-24 11:31:10 -04:00
if (Object.keys(newErrors).length > 0) {
setErrors(newErrors);
return true;
2024-11-12 10:19:03 -05:00
} else {
setErrors({});
return false;
}
2024-10-18 11:20:20 -04:00
}
return false;
};
export { buildErrors, hasValidationErrors };