forked from inveniosoftware/react-invenio-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflattenAndCategorizeErrors.js
42 lines (37 loc) · 1.51 KB
/
flattenAndCategorizeErrors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// This file is part of React-Invenio-Forms
// Copyright (C) 2025 CERN.
//
// React-Invenio-Forms is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.
export function flattenAndCategorizeErrors(obj, prefix = "") {
let flattenedErrors = {}; // To store flattened errors
let severityChecks = {}; // To store severity-based errors
for (let key in obj) {
let newKey = prefix ? `${prefix}.${key}` : key;
let value = obj[key];
if (value && typeof value === "object") {
if ("message" in value && "severity" in value) {
severityChecks[newKey] = value;
} else if (Array.isArray(value)) {
value.forEach((item, index) => {
let arrayKey = `${newKey}[${index}]`;
// Fix: If item is a string, store it directly instead of iterating over characters
if (typeof item === "string") {
flattenedErrors[arrayKey] = item;
} else {
let nested = flattenAndCategorizeErrors(item, arrayKey);
Object.assign(flattenedErrors, nested.flattenedErrors);
Object.assign(severityChecks, nested.severityChecks);
}
});
} else {
let nested = flattenAndCategorizeErrors(value, newKey);
Object.assign(flattenedErrors, nested.flattenedErrors);
Object.assign(severityChecks, nested.severityChecks);
}
} else {
flattenedErrors[newKey] = value;
}
}
return { flattenedErrors, severityChecks };
}