# @fastify/response-validation

> A simple plugin that enables response validation for Fastify.

Latest version **3.0.5** (published 2026-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fastify/response-validation
pnpm add @fastify/response-validation
yarn add @fastify/response-validation
bun add @fastify/response-validation
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.0.5 |
| Published | 2026-10-09 |
| First published | 2022-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 37.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 55 |
| Author | Tomas Della Vedova |
| Maintainers | matteo.collina, eomm, tony133 |
| Keywords | fastify, validation, response, json, schema |

## Links

- npm: https://www.npmjs.com/package/@fastify/response-validation
- Repository: https://github.com/fastify/fastify-response-validation
- Homepage: https://github.com/fastify/fastify-response-validation#readme
- Issues: https://github.com/fastify/fastify-response-validation/issues
- Funding: https://github.com/sponsors/fastify
- npm.io page: https://npm.io/package/@fastify/response-validation

## Dependencies (3)

- [ajv](https://npm.io/package/ajv.md) ^8.12.0
- [@fastify/error](https://npm.io/package/@fastify/error.md) ^4.0.0
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^6.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.0.5 (latest) — 2026-10-09
- 3.0.0 (next) — 2024-09-05
- 3.0.4 — 2026-08-08
- 3.0.3 — 2025-01-11
- 3.0.2 — 2024-11-06
- 3.0.1 — 2024-09-19
- 3.0.0-pre.fv5.2 — 2024-08-07
- 3.0.0-pre.fv5.1 — 2024-06-17
- 2.6.0 — 2024-03-20
- 2.5.1 — 2023-10-29
- 2.5.0 — 2023-08-07
- 2.4.1 — 2023-07-11
- 2.4.0 — 2023-07-02
- 2.3.1 — 2023-01-29
- 2.3.0 — 2023-01-01
- … 4 more at https://npm.io/package/@fastify/response-validation/versions

## README

# @fastify/response-validation

[![CI](https://github.com/fastify/fastify-response-validation/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-response-validation/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/response-validation.svg?style=flat)](https://www.npmjs.com/package/@fastify/response-validation)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

A simple plugin that enables response validation for Fastify.
The use of this plugin will slow down your overall performance, so we suggest using it only during development.

## Install
```
npm i @fastify/response-validation
```

## Usage
You just need to register the plugin and you will have response validation enabled:
```js
import fastify from 'fastify'

const app = fastify()

await app.register(require('@fastify/response-validation'))

app.route({
  method: 'GET',
  path: '/',
  schema: {
    response: {
      '2xx': {
        type: 'object',
        properties: {
          answer: { type: 'number' }
        }
      }
    }
  },
  handler: async (req, reply) => {
    return { answer: '42' }
  }
})

app.inject({
  method: 'GET',
  path: '/'
}, (err, res) => {
  if (err) throw err
  console.log(res.payload)
})
```

Different content types responses are supported by `@fastify/response-validation`, `@fastify/swagger`, and `fastify`. Please use `content` for the response otherwise Fastify itself will fail to compile the schema:
```js
{
  response: {
    200: {
      description: 'Description and all status-code based properties are working',
      content: {
        'application/json': {
          schema: {
            name: { type: 'string' },
            image: { type: 'string' },
            address: { type: 'string' }
          }
        },
        'application/vnd.v1+json': {
          schema: {
            fullName: { type: 'string' },
            phone: { type: 'string' }
          }
        }
      }
    }
  }
}
```

If you want to override the default [ajv](https://www.npmjs.com/package/ajv) configuration, you can do that by using the `ajv` option:
```js
// Default configuration:
//    coerceTypes: false
//    useDefaults: true
//    removeAdditional: true
//    allErrors: true

import responseValidator from '@fastify/response-validation'

// ... App setup

await fastify.register(responseValidator, {
  ajv: {
    coerceTypes: true
  }
})
```

You can also pass in an instance of ajv
```js
// Default configuration:
//    coerceTypes: false
//    useDefaults: true
//    removeAdditional: true
//    allErrors: true

import responseValidator from '@fastify/response-validation'
import Ajv from 'ajv'

// ... App setup

const ajv = new Ajv()
await fastify.register(responseValidator, { ajv })
```

By default, the response validation is enabled on every route that has a response schema defined. If needed you can disable it all together with `responseValidation: false`:
```js
import responseValidator from '@fastify/response-validation'

// ... App setup
await fastify.register(responseValidator, {
  responseValidation: false
})
```

Alternatively, you can disable a specific route with the same option:
```js
fastify.route({
  method: 'GET',
  path: '/',
  responseValidation: false,
  schema: {
    response: {
      '2xx': {
        type: 'object',
        properties: {
          answer: { type: 'number' }
        }
      }
    }
  },
  handler: async (req, reply) => {
    return { answer: '42' }
  }
})
```

## Plugins
You can also extend the functionalities of the ajv instance embedded in this validator by adding new ajv plugins.

```js
fastify.register(require('fastify-response-validation'), {
  ajv: {
    plugins: [
      require('ajv-formats'),
      [require('ajv-errors'), { singleError: false }]
      // Usage: [plugin, pluginOptions] - Plugin with options
      // Usage: plugin - Plugin without options
    ]
  }
})
```

## Errors

The errors emitted by this plugin are:

- `FST_RESPONSE_VALIDATION_FAILED_VALIDATION`: This error is emitted when a response does not conform to the provided schema.

- `FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED`: This error is emitted when there is no JSON schema available to validate the response.

## License
[MIT](./LICENSE)

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