Skip to content

Commit 4e92a91

Browse files
fix: handle numeric keys in deleteField (#1891)
* test: add failing test * fix: deleting numbers in array path works fine now * ci: apply automated fixes and generate docs * chore: add changset --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 7d19027 commit 4e92a91

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

.changeset/tasty-worms-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/form-core': patch
3+
---
4+
5+
Fix issue with deleteField and numeric keys

packages/form-core/src/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export function deleteBy(obj: any, _path: any) {
108108

109109
const key = path.shift()
110110

111-
if (typeof key === 'string') {
111+
if (
112+
typeof key === 'string' ||
113+
(typeof key === 'number' && !Array.isArray(parent))
114+
) {
112115
if (typeof parent === 'object') {
113116
return {
114117
...parent,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ describe('deleteBy', () => {
192192
expect(deleteBy(structure, 'kids[3].name')).toEqual(structure)
193193
expect(deleteBy(structure, 'nonexistent[3].nonexistent')).toEqual(structure)
194194
})
195+
196+
it('should now throw an error when deleting a path with numeric keys', () => {
197+
const structure2 = {
198+
123: 'a',
199+
b: {
200+
234: 'c',
201+
},
202+
d: {
203+
456: {
204+
e: 'f',
205+
},
206+
},
207+
}
208+
209+
expect(() => deleteBy(structure2, '123')).not.toThrow()
210+
expect(() => deleteBy(structure2, 'b.234')).not.toThrow()
211+
expect(() => deleteBy(structure2, 'd.456.e')).not.toThrow()
212+
})
195213
})
196214

197215
describe('makePathArray', () => {

0 commit comments

Comments
 (0)