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) {
export default function getComponentErrorKeys(name, formErrors, isNested = false) {
return Object.keys(formErrors) return Object.keys(formErrors)
.filter((errorKey) => { .filter(errorKey => errorKey.startsWith(name))
return take(errorKey.split('.'), isNested ? 2 : 1).join('.') === name; .map(errorKey =>
}) errorKey
.map((errorKey) => {
return errorKey
.split('.') .split('.')
.slice(0, name.split('.').length + 1) .slice(0, name.split('.').length + 1)
.join('.'); .join('.')
}); );
} }

View File

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