Skip to content

Commit

Permalink
Fix cleanPost(): koa-body 4.0.0+ puts files in ctx.request.files rath…
Browse files Browse the repository at this point in the history
…er than ctx.request.body.files
  • Loading branch information
chrisveness committed Jun 19, 2020
1 parent 1599b58 commit 9490766
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
13 changes: 4 additions & 9 deletions app-admin/app-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,10 @@ app.use(async function handleErrors(ctx, next) {

// clean up post data - trim & convert blank fields to null
app.use(async function cleanPost(ctx, next) {
if (ctx.request.body !== undefined) {
// koa-body puts multipart/form-data form fields in request.body.{fields,files}
const multipart = 'fields' in ctx.request.body && 'files' in ctx.request.body;
const body = multipart ? ctx.request.body.fields : ctx.request.body;
for (const key in body) {
if (typeof body[key] == 'string') {
body[key] = body[key].trim();
if (body[key] == '') body[key] = null;
}
for (const key in ctx.request.body) {
if (typeof ctx.request.body[key] == 'string') {
ctx.request.body[key] = ctx.request.body[key].trim();
if (ctx.request.body[key] == '') ctx.request.body[key] = null;
}
}
await next();
Expand Down
13 changes: 4 additions & 9 deletions app-www/app-www.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,10 @@ app.use(async function handleErrors(ctx, next) {

// clean up post data - trim & convert blank fields to null
app.use(async function cleanPost(ctx, next) {
if (ctx.request.body !== undefined) {
// koa-body puts multipart/form-data form fields in request.body.{fields,files}
const multipart = 'fields' in ctx.request.body && 'files' in ctx.request.body;
const body = multipart ? ctx.request.body.fields : ctx.request.body;
for (const key in body) {
if (typeof body[key] == 'string') {
body[key] = body[key].trim();
if (body[key] == '') body[key] = null;
}
for (const key in ctx.request.body) {
if (typeof ctx.request.body[key] == 'string') {
ctx.request.body[key] = ctx.request.body[key].trim();
if (ctx.request.body[key] == '') ctx.request.body[key] = null;
}
}
await next();
Expand Down

0 comments on commit 9490766

Please sign in to comment.