# restify-ajv-middleware

> json-schema middleware for restify using ajv for validation.

Latest version **0.0.3** (published 2016-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install restify-ajv-middleware
pnpm add restify-ajv-middleware
yarn add restify-ajv-middleware
bun add restify-ajv-middleware
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2016-08-16 |
| First published | 2016-06-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >4.0.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Matt Broadstone |
| Maintainers | mbroadst |

## Links

- npm: https://www.npmjs.com/package/restify-ajv-middleware
- Repository: https://github.com/mbroadst/restify-ajv-middleware
- Homepage: https://github.com/mbroadst/restify-ajv-middleware#readme
- Issues: https://github.com/mbroadst/restify-ajv-middleware/issues
- npm.io page: https://npm.io/package/restify-ajv-middleware

## Dependencies (2)

- [ajv](https://npm.io/package/ajv.md) ^4.1.0
- [restify-errors](https://npm.io/package/restify-errors.md) ^4.1.0

## Recent versions

- 0.0.3 (latest) — 2016-08-16
- 0.0.2 — 2016-06-01
- 0.0.1 — 2016-06-01

## README

# restify-ajv-middleware
[![Build Status](https://travis-ci.org/mbroadst/restify-ajv-middleware.svg?branch=master)](https://travis-ci.org/mbroadst/restify-ajv-middleware)
[![Test Coverage](https://codeclimate.com/github/mbroadst/restify-ajv-middleware/badges/coverage.svg)](https://codeclimate.com/github/mbroadst/restify-ajv-middleware/coverage)

A [json-schema](json-schema.org) validation middleware for restify using [ajv](https://github.com/epoberezkin/ajv). Inspired by [restify-joi-middleware](https://github.com/maxnachlinger/restify-joi-middleware)

### Installation
```
npm install restify-ajv-middleware --save
```

### Usage
```javascript
const restify = require('restify')
const validator = require('restify-ajv-middleware')
const server = restify.createServer()

server.use(validator(/* options */))

server.get({
  path: '/:id',
  validation: {
    params: {
      type: 'object',
      properties: { id: { type: 'number' } },
      required: [ 'id' ],
      additionalProperties: false
    }
  }
}, (req, res) => {
  return res.send(200, { id: req.params.id })
});
```

### Options
- _keysToValidate_: override the default keys to validate against
```javascript
server.use(validator({
  // changes the request keys validated
  keysToValidate: ['params', 'body', 'query', 'user', 'headers', 'trailers'],
});
```
- _errorResponder_: a function in the form `(transformedErr, req, res, next)` used to modify the default response strategy after failed validation.
```javascript
server.use(validator({
  // changes how errors are returned
  errorResponder: (transformedErr, req, res, next) => {
    res.send(400, transformedErr)
    return next()
  }
});
```
- _errorTransformer_: a function in the form `(validationInput, errors)`, used to transform the error generated after failed validation
```javascript
server.use(validator({
  // changes how json-schema errors are transformed
  errorTransformer: (validationInput, errors) => new restifyErrors.BadRequestError('Something else'),
});
```
- _ajv_: options passed to the internal ajv instance. See [options](https://github.com/epoberezkin/ajv#options) for more info.

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