diff --git a/.changeset/tasty-worms-sniff.md b/.changeset/tasty-worms-sniff.md new file mode 100644 index 000000000..769ff8c94 --- /dev/null +++ b/.changeset/tasty-worms-sniff.md @@ -0,0 +1,5 @@ +--- +'@tanstack/form-core': patch +--- + +Fix issue with deleteField and numeric keys diff --git a/packages/form-core/src/utils.ts b/packages/form-core/src/utils.ts index d6729adee..cf9383d84 100644 --- a/packages/form-core/src/utils.ts +++ b/packages/form-core/src/utils.ts @@ -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, diff --git a/packages/form-core/tests/utils.spec.ts b/packages/form-core/tests/utils.spec.ts index 42680d69e..229e7cdd9 100644 --- a/packages/form-core/tests/utils.spec.ts +++ b/packages/form-core/tests/utils.spec.ts @@ -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', () => {