2024-10-18 11:20:20 -04:00
|
|
|
const buildErrors = (prev, id, error) => {
|
|
|
|
|
const updatedErrors = { ...prev };
|
|
|
|
|
if (error) {
|
2024-10-21 10:02:36 -04: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",
|
|
|
|
|
].includes(err.path[0])
|
|
|
|
|
) {
|
|
|
|
|
newErrors[err.path[0]] = err.message ?? "Validation error";
|
|
|
|
|
}
|
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;
|
|
|
|
|
} else return false;
|
2024-10-18 11:20:20 -04:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2024-10-22 09:37:39 -04:00
|
|
|
export { buildErrors, hasValidationErrors };
|