Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-worms-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/form-core': patch
---

Fix issue with deleteField and numeric keys
5 changes: 4 additions & 1 deletion packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export function deleteBy(obj: any, _path: any) {

const key = path.shift()

if (typeof key === 'string') {
if (
typeof key === 'string' ||
(typeof key === 'number' && !Array.isArray(parent))
) {
if (typeof parent === 'object') {
return {
...parent,
Expand Down
18 changes: 18 additions & 0 deletions packages/form-core/tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ describe('deleteBy', () => {
expect(deleteBy(structure, 'kids[3].name')).toEqual(structure)
expect(deleteBy(structure, 'nonexistent[3].nonexistent')).toEqual(structure)
})

it('should now throw an error when deleting a path with numeric keys', () => {
const structure2 = {
123: 'a',
b: {
234: 'c',
},
d: {
456: {
e: 'f',
},
},
}

expect(() => deleteBy(structure2, '123')).not.toThrow()
expect(() => deleteBy(structure2, 'b.234')).not.toThrow()
expect(() => deleteBy(structure2, 'd.456.e')).not.toThrow()
})
})

describe('makePathArray', () => {
Expand Down
Loading