koa-paramcheck
TypeScript icon, indicating that this package has built-in type declarations

0.4.2 • Public • Published

koa-paramcheck

Koa middlewares, for parsing and checking query string or JSON body.

npm version license build coverage

Description

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.

Install

npm install koa-paramcheck

Example

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]"}

Documentation

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i koa-paramcheck

Weekly Downloads

19

Version

0.4.2

License

MIT

Unpacked Size

50.4 kB

Total Files

11

Last publish

Collaborators

  • songshuangfei