foal
foal copied to clipboard
Simplify `ValidateMultipartFormDataBody` and support optional fields
Issue
- The hooks does not support optional fields (issue: #1008).
- There is no error returned to the client when multiple files are uploaded whereas we expect a single one.
- The parameters passed to the hook are verbose even when uploading a simple file.
- We have a very large number of nested objects to access the files and fields in
ctx
:ctx.request.body.fields.name
andctx.request.body.files.avatar
. - The typing is not great in the controller method body because we have to "cast" variables:
ctx.request.body.files.avatar as File|undefined
. - The name
ValidateMultipartFormDataBody
is not meaningful to someone reading the code: it shows the type of requests that are made but does not describe the really purpose of the hook.
New features
-
Context.files
is an instance ofFileList
which is a class to listFiles
(s). It has mainly two methods:ctx.files.push('avatar', file')
andctx.files.get('avatar')
which always returns an array ofFiles
(s) (that might be empty). - New error
MULTIPLE_FILES_NOT_ALLOWED
(the valuefiles.x
is not replaced anymore). - The interface of the hook has changed and accepts optional fields:
@ParseAndValidateFiles( { profile: { required: true } }, { type: 'object', properties: { description: { type: 'string' } }, required: ['description'], additionalProperties: false } )
-
ctx.files
which is properly typed
Breaking changes
-
ValidateMultipartFormDataBody
is renamedParseAndValidateFiles
- Code is compiled to es2019 which are supported by Node 14 and 16.
-
File
is exported from@foal/core
and not@foal/storage
anymore. -
ctx.request.body.fields
becomesctx.request.body
. -
ctx.request.body.files.avatar as File
becomesctx.files.get('avatar')[0]
-
ctx.request.body.files.avatars as File[]
becomesctx.files.get('avatars')
- The parameters of the hook have changed.
-
Context.state
andContext.request
arereadonly
: they cannot be re-assigned. - Previously
saveTo: ''
was regarded as an upload with buffer.
Dependencies
-
@foal/storage
Checklist
- [x] Add/update/check docs (code comments and docs/ folder).
- [x] Add/update/check tests.
- [x] Update/check the cli generators.
Codecov Report
Merging #1048 (dad85c6) into v3-0-0 (120a7e0) will increase coverage by
0.01%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## v3-0-0 #1048 +/- ##
==========================================
+ Coverage 98.84% 98.86% +0.01%
==========================================
Files 92 95 +3
Lines 1736 1763 +27
Branches 418 422 +4
==========================================
+ Hits 1716 1743 +27
Misses 20 20
Impacted Files | Coverage Δ | |
---|---|---|
packages/core/src/common/file/file.ts | 100.00% <ø> (ø) |
|
packages/core/src/index.ts | 0.00% <ø> (ø) |
|
packages/core/src/common/file/file-list.ts | 100.00% <100.00%> (ø) |
|
packages/core/src/common/file/index.ts | 100.00% <100.00%> (ø) |
|
packages/core/src/common/index.ts | 100.00% <100.00%> (ø) |
|
packages/core/src/core/http/context.ts | 100.00% <100.00%> (ø) |
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.