Koa middlewares, for parsing and checking query string or JSON body.
Koa middlewares for parsing and checking query and JSON body. Define a rule for this middleware as the first parameter. If the parameters of the http request do not match this rule, http will response with a 400 status code and a detailed error message.
npm install koa-paramcheck
const Koa = require('koa');
const { queryCheck } = require('koa-paramcheck');
const app = new Koa();
app.use(queryCheck({
properties: {
keyword: {
type: 'string',
allowEmpty: false
},
page: {
type: 'number',
min: 1
},
pageSize: {
type: 'number',
min: 1,
max: 20
}
},
requiredKeys: ['page', 'pageSize']
})).use(async (ctx) => {
console.log(ctx.request.passedParams.query);
ctx.body = '';
});
app.listen(3000);
Test.
curl "localhost:3000?page=1&pageSize=30"
Response.
status: 400 Bad Request
body: {"queryErrors":"pageSize does not in range [1, 20]"}
MIT