Skip to content

Commit f91519a

Browse files
authored
fix: delete non-existent fields without errors (#550)
1 parent f264865 commit f91519a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

packages/form-core/src/tests/utils.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@ describe('deleteBy', () => {
7070
expect(deleteBy(structure, 'kids.0.name').kids[0].name).not.toBeDefined()
7171
expect(deleteBy(structure, 'kids.0.age').kids[0].age).not.toBeDefined()
7272
})
73+
74+
it('should delete non-existent paths like a noop', () => {
75+
expect(deleteBy(structure, 'nonexistent')).toEqual(structure)
76+
expect(deleteBy(structure, 'nonexistent.nonexistent')).toEqual(structure)
77+
expect(deleteBy(structure, 'kids.3.name')).toEqual(structure)
78+
expect(deleteBy(structure, 'nonexistent.3.nonexistent')).toEqual(structure)
79+
})
7380
})

packages/form-core/src/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function deleteBy(obj: any, _path: any) {
7676
const path = makePathArray(_path)
7777

7878
function doDelete(parent: any): any {
79+
if (!parent) return;
7980
if (path.length === 1) {
8081
const finalPath = path[0]!
8182
const { [finalPath]: remove, ...rest } = parent
@@ -95,6 +96,9 @@ export function deleteBy(obj: any, _path: any) {
9596

9697
if (typeof key === 'number') {
9798
if (Array.isArray(parent)) {
99+
if (key >= parent.length) {
100+
return parent
101+
}
98102
const prefix = parent.slice(0, key)
99103
return [
100104
...(prefix.length ? prefix : new Array(key)),

0 commit comments

Comments
 (0)