Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.3.x] Fix array filtering of files #15

Merged
merged 1 commit into from
Jun 8, 2023
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
2 changes: 1 addition & 1 deletion packages/core/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ const forgetFiles = (data: Record<string, unknown>): Record<string, unknown> =>
}

if (Array.isArray(value)) {
newData[name] = value.filter(isFile)
newData[name] = value.filter((value) => !isFile(value))

return
}
Expand Down
65 changes: 65 additions & 0 deletions packages/core/tests/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,68 @@ it('is valid after field has changed and successful validation has triggered', a
expect(requestMade).toBe(true)
expect(validator.valid()).toEqual(['name'])
})

it('filters out files', () => {
let config
axios.request.mockImplementationOnce((c) => {
config = c
return Promise.resolve({ headers: { precognition: 'true' } })
})
const validator = createValidator((client) => client.post('/foo', {
name: 'Tim',
email: null,
fruits: [
'apple',
'banana',
new Blob([], { type: 'image/png' }),
],
avatar: new Blob([], { type: 'image/png' }),
nested: {
name: 'Tim',
email: null,
fruits: [
'apple',
'banana',
new Blob([], { type: 'image/png' }),
],
avatar: new Blob([], { type: 'image/png' }),
nested: {
name: 'Tim',
email: null,
fruits: [
'apple',
'banana',
new Blob([], { type: 'image/png' }),
],
avatar: new Blob([], { type: 'image/png' }),
}
}
}))

validator.validate('text', 'Tim')

expect(config.data).toEqual({
name: 'Tim',
email: null,
fruits: [
'apple',
'banana',
],
nested: {
name: 'Tim',
email: null,
fruits: [
'apple',
'banana',
],
nested: {
name: 'Tim',
email: null,
fruits: [
'apple',
'banana',
],
}
}
})
})