Skip to content

Commit

Permalink
body with files
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyou2012 committed Mar 5, 2019
1 parent 9cceee2 commit 398ad05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 26 additions & 3 deletions examples/petstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ mapper.addServer({
description: 'Development'
});

mapper.get('/', ctx => {
ctx.body = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/pet/123/uploadImage">
<input name="name" type="text" />
<input name="image" type="file" />
<input type="submit" value="submit" />
</form>
</body>
</html>
`;
});

mapper.post('/pet', {
tags: ['pet'],
summary: 'Add a new pet to the store',
Expand Down Expand Up @@ -199,20 +218,22 @@ mapper.del('/pet/:petId', {
}, (ctx) => { ctx.body = {}; });

mapper.post('/pet/:petId/uploadImage', {
bodyparser: { multipart: true },
tags: ['pet'],
summary: 'uploads an image',
operationId: 'uploadFile',
params: {
petId: {
type: 'integer',
format: 'int64',
// format: 'int64',
description: 'ID of pet to update',
required: true
}
},
body: {
image: {
type: 'file'
type: 'file',
required: true
}
},
responses: {
Expand All @@ -222,7 +243,9 @@ mapper.post('/pet/:petId/uploadImage', {
}
}
}
}, (ctx) => { ctx.body = {}; });
}, (ctx) => {
ctx.body = {};
});

mapper.schema('Tag', {
id: { type: 'integer', format: 'int64' },
Expand Down
5 changes: 4 additions & 1 deletion src/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ export default class Layer {
this.bodyValidate = validator.compile(this.bodySchema);
}
const { throwBodyError } = this.opts;
const valid = this.bodyValidate(ctx.request.body);
const valid = this.bodyValidate({
...ctx.request.body,
...ctx.request.files
});
ctx.bodyErrors = this.bodyValidate.errors;
if (!valid && throwBodyError !== false) {
if (typeof throwBodyError === 'function') {
Expand Down

0 comments on commit 398ad05

Please sign in to comment.