Files
Checkmate/src/Validation/error.js
T

102 lines
3.4 KiB
JavaScript
Raw Normal View History

/**
* Update errors if passed id matches the error.details[0].path, otherwise remove
* the error for the id
* @param {*} prev Previous errors *
* @param {*} id ID of the field whose error is to be either updated or removed
* @param {*} error the error object
* @returns the Update Errors with the specific field with id being either removed or updated
*/
2024-10-18 11:20:20 -04:00
const buildErrors = (prev, id, error) => {
const updatedErrors = { ...prev };
if (error && id == error.details[0].path) {
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;
};
2024-12-04 23:51:51 +08:00
/**
* Processes Joi validation errors and returns a filtered object of error messages for fields that have been touched.
*
* @param {Object} validation - The Joi validation result object.
* @param {Object} validation.error - The error property of the validation result containing details of validation failures.
* @param {Object[]} validation.error.details - An array of error details from the Joi validation. Each item contains information about the path and the message.
* @param {Object} touchedErrors - An object representing which fields have been interacted with. Keys are field IDs (field names), and values are booleans indicating whether the field has been touched.
* @returns {Object} - An object where keys are the field IDs (if they exist in `touchedErrors` and are in the error details) and values are their corresponding error messages.
*/
const getTouchedFieldErrors = (validation, touchedErrors) => {
let newErrors = {};
if (validation?.error) {
newErrors = validation.error.details.reduce((errors, detail) => {
const fieldId = detail.path[0];
if (touchedErrors[fieldId] && !(fieldId in errors)) {
errors[fieldId] = detail.message;
}
return errors;
}, {});
}
return newErrors;
};
/**
*
* @param {*} form The form object of the submitted form data
* @param {*} validation The Joi validation rules
* @param {*} setErrors The function used to set the local errors
* @returns true if there is no error or false if there is error after validating the form
* the error will be reset to {} if returns false; otherwise the errors object will be set with
* the new value
*/
2024-10-18 11:20:20 -04:00
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",
"_id",
"__v",
"createdAt",
"updatedAt"
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
2024-11-15 14:49:27 -05:00
if (!form.cpu || form.usage_cpu) {
2024-11-14 11:01:36 -05:00
newErrors["usage_cpu"] = null;
2024-11-12 10:19:03 -05:00
}
2024-11-15 14:49:27 -05:00
if (!form.memory || form.usage_memory) {
2024-11-14 11:01:36 -05:00
newErrors["usage_memory"] = null;
2024-11-12 10:19:03 -05:00
}
2024-11-15 14:49:27 -05:00
if (!form.disk || form.usage_disk) {
2024-11-14 11:01:36 -05:00
newErrors["usage_disk"] = null;
2024-11-12 10:19:03 -05:00
}
if (!form.temperature || form.usage_temperature) {
newErrors["usage_temperature"] = null;
}
2024-10-18 11:20:20 -04:00
});
if (Object.values(newErrors).some((v) => v)) {
2024-10-24 11:31:10 -04:00
setErrors(newErrors);
return true;
2024-11-12 10:19:03 -05:00
} else {
setErrors({});
return false;
}
2024-10-18 11:20:20 -04:00
}
setErrors({});
2024-10-18 11:20:20 -04:00
return false;
};
2024-12-04 23:51:51 +08:00
export { buildErrors, hasValidationErrors, getTouchedFieldErrors };