Skip to content

Commit 0329210

Browse files
[0.5.x] Improve forgetFiles to handle nested structures and edge cases (#119)
* Improve forgetFiles to handle nested structures and edge cases - Added recursive support to remove files from deeply nested arrays and objects. - Improved compatibility with non-iterable objects using Object.getOwnPropertyNames. * fix: remove files from nested arrays and objects without leaving empty values * test: add coverage for deeply nested files (files in objects within arrays) * formatting --------- Co-authored-by: rottifant <[email protected]>
1 parent 7ce485b commit 0329210

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/core/src/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ const forgetFiles = (data: Record<string, unknown>): Record<string, unknown> =>
453453
}
454454

455455
if (Array.isArray(value)) {
456-
newData[name] = value.filter((value) => !isFile(value))
456+
newData[name] = Object.values(forgetFiles({ ...value }))
457457

458458
return
459459
}

packages/core/tests/validator.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ it('filters out files', async () => {
260260
'apple',
261261
'banana',
262262
new Blob([], { type: 'image/png' }),
263+
['nested', new Blob([], { type: 'image/png' })],
264+
{
265+
name: 'Tim',
266+
email: null,
267+
avatar: new Blob([], { type: 'image/png' }),
268+
},
263269
],
264270
avatar: new Blob([], { type: 'image/png' }),
265271
nested: {
@@ -292,6 +298,11 @@ it('filters out files', async () => {
292298
fruits: [
293299
'apple',
294300
'banana',
301+
['nested'],
302+
{
303+
name: 'Tim',
304+
email: null,
305+
},
295306
],
296307
nested: {
297308
name: 'Tim',

0 commit comments

Comments
 (0)