1.0.8 • Published 3 years ago

koa-router-checker v1.0.8

Weekly downloads
12
License
MIT
Repository
github
Last release
3 years ago

koa-router-checker

Validator middleware for koa-router.

Install

npm i koa-router-checker --save

Example

'use strict';

const Koa = require('koa');
const Router = require('koa-router');
const koaRouterChecker = require('koa-router-checker');
const bodyparser = require('koa-bodyparser');
const app = new Koa()
const router = new Router()

app.use(bodyparser());


router.get('/', koaRouterChecker({
  // message [String, Function(key: string)] low priority
  message: function(key: string) {
    ...
  },
  // status [Number] low priority
  status: 400,

 /**
  * ctx.body
  * ctx.query
  * ctx.headers
  * ....
 */
  rule: {
    // body: ['name','password'] surrport Array
    body: {
      name: {
        status: 400,
        //[String, Function(key: id)] // high priority
        message: 'name is required'
      }
    },
    query: {
      id: {
        status: 400,
        //[String, Function(key: id)] // high priority
        message: 'id is required'
      }
    },
    headers: {
      token: {
        status: 401,
        // message [String, Function(key: token)] // high priority
        message: 'no auth'
      }
    }

    ...


  }
}), async(ctx,next) => {
  ...

  await next()

  ....
});

app.use(router.routes());

app.listen(5000);
// ctx.body
ctx.status = 400  //  default 400, the status of define
ctx.body = {
  message: 'xx is required' // default key is required, the message of define
}

Test

see test.

npm run test
1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.0

3 years ago