0.4.2 • Published 4 years ago

koa-paramcheck v0.4.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.7

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago