# koa-better-validator

> a koa validator

Latest version **1.1.2** (published 2020-04-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install koa-better-validator
pnpm add koa-better-validator
yarn add koa-better-validator
bun add koa-better-validator
```

## 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.1.2 |
| Published | 2020-04-29 |
| First published | 2020-03-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 2 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Author | DMHB |
| Maintainers | dmhb |
| Keywords | koa, koa2, validator, better, XSS |

## Links

- npm: https://www.npmjs.com/package/koa-better-validator
- Homepage: https://github.com/Dm-China/koa-better-validator.git
- npm.io page: https://npm.io/package/koa-better-validator

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [validator](https://npm.io/package/validator.md) ^12.2.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2020-04-29
- 1.1.0 — 2020-03-20
- 1.0.9 — 2020-03-20
- 1.0.8 — 2020-03-20
- 1.0.7 — 2020-03-20
- 1.0.6 — 2020-03-20
- 1.0.5 — 2020-03-20
- 1.0.4 — 2020-03-20
- 1.0.3 — 2020-03-20
- 1.0.2 — 2020-03-19
- 1.0.1 — 2020-03-19
- 1.0.0 — 2020-03-19

## README

### Installation
---
The library was inspired by the [validator.js](https://github.com/validatorjs/validator.js).

```javascript
$ npm i koa-better-validator -S
```
or
```javascript
$ yarn add koa-better-validator -S
```

### Usage
---
```javascript
const Koa = require('koa')
const { koaValidator, KoaValidatorException } = require('koa-better-validator')
const app = new Koa()

// catch exception
app.use(async (ctx, next) => {
  try {
    await next()
  } catch (e) {
    if(e instanceof KoaValidatorException) {
      ctx.body = {
        errMsg: e.errMsg || 'This is koa-better-validator catch errors'
      } 
    }
  } 
})

// customize you validator
app.use(koaValidator({
  customValidators: {
    isString (param) {
      return Object.prototype.toString.call(param) === '[object String]' ? param : false;
    }
    // other customValidators
}
}))

app.use(async (ctx, next) => {
  // The method verify will automatically identify from body、params、query、header
  // ctx.query, ctx.params, ctx.request.body(if you have used a library like koa-bodyparser), headers
  ctx.verify('name', 'the name length should be between 6 and 10 ').isLength({min: 6, max: 10})
  const { name } = await ctx.valid()
  console.log(name)
  await next()
})

app.listen(3000)
```

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