# koa-router-checker

> a params validator for koa-router

Latest version **1.0.8** (published 2021-01-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-router-checker
pnpm add koa-router-checker
yarn add koa-router-checker
bun add koa-router-checker
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2021-01-13 |
| First published | 2021-01-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 0 |
| Unpacked size | 5.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | zhangjiale |
| Maintainers | zhangjialegh |
| Keywords | validator, checker, koa, koa-router |

## Links

- npm: https://www.npmjs.com/package/koa-router-checker
- Repository: https://github.com/zhangjialegh/koa-router-checker
- Issues: https://github.com/zhangjialegh/koa-router-checker/issues
- npm.io page: https://npm.io/package/koa-router-checker

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.8 (latest) — 2021-01-13
- 1.0.7 — 2021-01-13
- 1.0.6 — 2021-01-13
- 1.0.5 — 2021-01-12
- 1.0.0 — 2021-01-12

## README

# koa-router-checker

#### Validator middleware for [koa-router](https://github.com/koajs/router).

## Install

```js
npm i koa-router-checker --save
```

## Example

```js
'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);
```

```js
// 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](https://github.com/zhangjialegh/koa-router-checker/blob/master/test/middleware.test.js).

```js
npm run test
```

---
_Source: https://npm.io/package/koa-router-checker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
