-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
33 lines (30 loc) · 1.02 KB
/
index.ts
File metadata and controls
33 lines (30 loc) · 1.02 KB
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
import { FormFieldError } from '@shared/config';
import { FormInstance } from 'antd';
/**
* Util for processing and displaying form field errors from backend
*
* @param form - Form instance
* @param errors - Array of objects with paths to fields contains an error from backend
*
*/
export const showErrorsInFields = (form: FormInstance, errors: FormFieldError[]): void => {
const fieldErrors = errors
.map((field) => {
// Keep only valid parts of the path
const validPath = field.location.reduce<Array<string | number>>((acc, key) => {
const currentPath = [...acc, key];
// Check if the path to the current key exists
if (typeof form.getFieldValue(currentPath) === 'object' || form.isFieldTouched(currentPath)) {
acc.push(key);
}
return acc;
}, []);
return {
name: validPath,
errors: [field.message],
};
})
// Remove errors without a valid path
.filter(({ name }) => name.length > 0);
form.setFields(fieldErrors);
};