# koa2-validate

> Validation middleware for koa2 request params with help of validator package

Latest version **1.0.1** (published 2019-01-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install koa2-validate
pnpm add koa2-validate
yarn add koa2-validate
bun add koa2-validate
```

## 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.1 |
| Published | 2019-01-21 |
| First published | 2019-01-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Author | Armen Hajian |
| Maintainers | armenhajian |
| Keywords | validator, validation, validate, middleware, koa2 |

## Links

- npm: https://www.npmjs.com/package/koa2-validate
- Repository: https://github.com/armenhajian/koa2-validate
- Issues: https://github.com/armenhajian/koa2-validate/issues
- npm.io page: https://npm.io/package/koa2-validate

## Dependencies (1)

- [validator](https://npm.io/package/validator.md) ^10.11.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-01-21
- 1.0.0 — 2019-01-18

## README

# koa2-validate

[![Build Status](https://img.shields.io/npm/v/koa2-validate.svg)](https://www.npmjs.com/package/koa2-validate)

A library that wrap [validator.js][vldtr.git] package for koa.js to validate request parameters.

### Installation and usage

The lib requires [Koa.js](https://koajs.com/) to run.


```sh
$ npm install koa2-validate
```

#### No ES6

```javascript
var validate = require('koa2-validate');
```

#### ES6

```javascript
import validate from 'koa2-validate';
```
##### Usage example
#
```javascript
const app = new Koa()
  .use(cors())
  .use(bodyParser())
  .use(validate()) //use before api routes!!
  .use(api.routes())
  

/**
Use as {param: 'required|methodNameWithoutPrefix'}
*/
api.get('/simple-list',
  async (ctx, next) => {
    ctx.validate({
      limit: 'required|numeric^{no_symbols: true}',
      page: 'required|numeric^{no_symbols: true}'
    })
})
```

The `|` symbol is a separator for rules.\
Only `required` is build-in rule.\
For other rules use method name from the Validate.js [validators][vldrs-link] which STARTS with `is` prefix.\
For example if you write `'..|numeric'` it will call `isNumeric` method from `Validate.js` lib. &nbsp;
For options you can use `^{}`.\
e.g. if you want to call `isNumeric(myVar, {no_symbols: true})` then use `..|numeric^{no_symbols: true}`

If the params not pass the rules it will respond with `422` status and error text `${key} should be ${rule}`

#### ToDo
- Make error texts more dynamic (maybe with option to pass from code)
- Add support of methods without `is` prefix
- Write tests

License
----

MIT


[//]: # (These are reference links used in the body of this note and get stripped out when the markdown processor does its job. There is no need to format nicely because it shouldn't be seen. Thanks to amazing https://dillinger.io builder)


   [vldtr.npm]: <https://www.npmjs.com/package/validator>
   [vldtr.git]: <https://github.com/chriso/validator.js>
   [vldrs-link]: <https://github.com/chriso/validator.js#validators>

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