Skip to content

Commit a9fb991

Browse files
DinaYakovlevidanto
authored andcommitted
Add koa support (#40)
Add koa support
1 parent bcc32b5 commit a9fb991

26 files changed

Lines changed: 3362 additions & 445 deletions

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ node_js:
33
- "6"
44
- "8"
55
- "node"
6-
script: "npm test"
6+
script: >
7+
node_version=$(node -v);
8+
if [ ${node_version:1:1} -ne 6 ]; then
9+
npm test
10+
else
11+
npm run node6-test
12+
fi
713
after_success: >
814
node_version=$(node -v);
915
if [ ${node_version:1:1} -ne 6 ]; then
1016
npm run coveralls
11-
fi
17+
fi

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The function return Promise.
6262
##### Options
6363

6464
Options currently supports:
65+
- `framework` - Defines in which framework the middleware is working ('koa' or 'express'). As default, set to 'express'.
6566
- `formats` - Array of formats that can be added to `ajv` configuration, each element in the array should include `name` and `pattern`.
6667
- `beautifyErrors`- Boolean that indicates if to beautify the errors, in this case it will create a string from the Ajv error.
6768
- Examples:
@@ -89,7 +90,7 @@ formats: [
8990
```
9091

9192
## Usage Example
92-
93+
### Express
9394
```js
9495
swaggerValidator.init('test/unit-tests/input-validation/pet-store-swagger.yaml')
9596
.then(function () {
@@ -115,11 +116,45 @@ swaggerValidator.init('test/unit-tests/input-validation/pet-store-swagger.yaml')
115116
});
116117
});
117118
```
119+
### Koa
120+
```js
121+
'use strict';
122+
const Koa = require('koa');
123+
const Router = require('koa-router');
124+
const bodyParser = require('koa-bodyparser');
125+
const inputValidation = require('../../src/middleware');
126+
let app = new Koa();
127+
let router = new Router();
128+
app.use(bodyParser());
129+
app.use(router.routes());
130+
module.exports = inputValidation.init('test/pet-store-swagger.yaml', {framework: 'koa'})
131+
.then(function () {
132+
router.get('/pets', inputValidation.validate, async function(ctx, next) {
133+
ctx.status = 200;
134+
ctx.body = { result: 'OK' };
135+
});
136+
router.post('/pets', inputValidation.validate, async function (ctx, next) {
137+
ctx.status = 200;
138+
ctx.body = { result: 'OK' };
139+
});
140+
router.get('/pets/:petId', inputValidation.validate, async function (ctx, next) {
141+
ctx.status = 200;
142+
ctx.body = { result: 'OK' };
143+
});
144+
router.put('/pets', inputValidation.validate, async function (ctx, next) {
145+
ctx.status = 200;
146+
ctx.body = { result: 'OK' };
147+
});
118148

149+
return Promise.resolve(app);
150+
});
151+
```
119152
## Important Notes
120153

121154
- Objects - it is important to set any objects with the property `type: object` inside your swagger file, although it isn't a must in the Swagger (OpenAPI) spec in order to validate it accurately with [ajv](https://www.npmjs.com/package/ajv) it must be marked as `object`
122155
- multipart/form-data (files) supports is based on [`express/multer`](https://github.com/expressjs/multer)
156+
- koa support - When using this package as middleware for koa, the validations errors are being thrown.
157+
- koa packages - This package supports koa server that uses [`koa-router`](https://www.npmjs.com/package/koa-router), [`koa-bodyparser`](https://www.npmjs.com/package/koa-bodyparser) and [`koa-multer`](https://www.npmjs.com/package/koa-multer)
123158

124159
## Running Tests
125160
Using mocha, istanbul and mochawesome

0 commit comments

Comments
 (0)