RepeatableComponent: Simplify error handling and fix all nesting levels

This commit is contained in:
Gustav Hansen 2022-03-29 16:02:56 +02:00
parent 1f811ed9a7
commit c3383c63b1
2 changed files with 7 additions and 11 deletions

View File

@ -1,14 +1,10 @@
import take from 'lodash/take';
export default function getComponentErrorKeys(name, formErrors, isNested = false) {
export default function getComponentErrorKeys(name, formErrors) {
return Object.keys(formErrors)
.filter((errorKey) => {
return take(errorKey.split('.'), isNested ? 2 : 1).join('.') === name;
})
.map((errorKey) => {
return errorKey
.filter(errorKey => errorKey.startsWith(name))
.map(errorKey =>
errorKey
.split('.')
.slice(0, name.split('.').length + 1)
.join('.');
});
.join('.')
);
}

View File

@ -19,7 +19,7 @@ describe('getComponentErrorKeys', () => {
'parent.child.1.field': 'validation-error',
};
expect(getComponentErrorKeys('parent.child', FIXTURE, true)).toStrictEqual([
expect(getComponentErrorKeys('parent.child', FIXTURE)).toStrictEqual([
'parent.child.0',
'parent.child.1',
]);