From 398ad05f08e7dc8f386a84f06b198bf4a3ade658 Mon Sep 17 00:00:00 2001 From: helloyou2012 Date: Tue, 5 Mar 2019 23:47:38 +0800 Subject: [PATCH] body with files --- examples/petstore.js | 29 ++++++++++++++++++++++++++--- src/layer.js | 5 ++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/examples/petstore.js b/examples/petstore.js index b3f7cbf..e8b394d 100644 --- a/examples/petstore.js +++ b/examples/petstore.js @@ -43,6 +43,25 @@ mapper.addServer({ description: 'Development' }); +mapper.get('/', ctx => { + ctx.body = ` + + + + + Document + + +
+ + + +
+ + + `; +}); + mapper.post('/pet', { tags: ['pet'], summary: 'Add a new pet to the store', @@ -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: { @@ -222,7 +243,9 @@ mapper.post('/pet/:petId/uploadImage', { } } } -}, (ctx) => { ctx.body = {}; }); +}, (ctx) => { + ctx.body = {}; +}); mapper.schema('Tag', { id: { type: 'integer', format: 'int64' }, diff --git a/src/layer.js b/src/layer.js index f2997ef..5b2fa66 100644 --- a/src/layer.js +++ b/src/layer.js @@ -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') {