# koa-joi-validator

> A simple Joi validator for koa router

Latest version **4.0.0** (published 2016-02-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-joi-validator
pnpm add koa-joi-validator
yarn add koa-joi-validator
bun add koa-joi-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 | 4.0.0 |
| Published | 2016-02-10 |
| First published | 2015-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Jordan Van Walleghem |
| Maintainers | vanwalj |
| Keywords | koa, joi, body, header, parser, validator |

## Links

- npm: https://www.npmjs.com/package/koa-joi-validator
- Repository: https://github.com/vanwalj/koa-joi-validator
- Homepage: https://github.com/vanwalj/koa-joi-validator#readme
- Issues: https://github.com/vanwalj/koa-joi-validator/issues
- npm.io page: https://npm.io/package/koa-joi-validator

## Dependencies (3)

- [joi](https://npm.io/package/joi.md) 7.2.3
- [co-body](https://npm.io/package/co-body.md) 4.0.0
- [bluebird](https://npm.io/package/bluebird.md) 3.2.2

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2016-02-10
- 5.1.3 (next) — 2016-11-17
- 5.1.2 — 2016-11-17
- 5.0.2 — 2016-04-15
- 5.0.1 — 2016-04-15
- 5.0.0 — 2016-04-15
- 3.0.1 — 2016-02-09
- 3.0.0 — 2016-02-08
- 2.0.1 — 2016-01-31
- 2.0.0 — 2016-01-31
- 1.1.0 — 2016-01-31
- 1.0.0 — 2015-11-29

## README

# koa-joi-validator
A simple validator for your awesome koa application

## API

```validator.Joi```
The package Joi version (prefer this version over your custom version in case of breaking changes)

```validator.validate(options)```
A koa middleware which will validate and transform the request

| param | type | required | description |
|-------|------|------------------|-------------|
| options | {} | true | |
| options.type | string | Only if you want the request body to be parsed | the coBody parser to call, can be `json` `form` or `text` |
| options.failure | int | false | The error code to throw on case of validation error, default to 400 |
| options.options | {} | false | Options passed to Joi validator, such as `allowUnknown` |
| options.body | Joi.object | false | A joi schema validated against the request body | 
| options.params | Joi.object | false | A joi schema validated against the request params | 
| options.headers | Joi.object | false | A joi schema validated against the request headers | 
| options.query | Joi.object | false | A joi schema validated against the request query | 
 
## Usage

``` es6
router.post('/:number/:string/:date',
  validator.validate({
    type: 'json',
    params: {
      number: validator.Joi.number().required(),
      string: validator.Joi.string().required(),
      date: validator.Joi.date().required()
    },
    body: {
      number: validator.Joi.number().required(),
      string: validator.Joi.string().required(),
      date: validator.Joi.date().required()
    },
    headers: validator.Joi.object({
      number: validator.Joi.number().required(),
      string: validator.Joi.string().required(),
      date: validator.Joi.date().required()
    }).options({ allowUnknown: true }),
    query: {
      number: validator.Joi.number().required(),
      string: validator.Joi.string().required(),
      date: validator.Joi.date().required()
    }
  }),
  function * () {
    this.assert(typeof this.params.number === 'number');
    this.assert(typeof this.params.string === 'string');
    this.assert(this.params.date instanceof Date);

    ['body', 'headers', 'query'].forEach(el => {
      this.assert(typeof this.request[el].number === 'number');
      this.assert(typeof this.request[el].string === 'string');
      this.assert(this.request[el].date instanceof Date);
    });

    this.status = 204;
  }
);
```

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