https://github.com/fastify/fastify-response-validation
A simple plugin that enables response validation for Fastify
https://github.com/fastify/fastify-response-validation
ajv fastify fastify-plugin response validation
Last synced: about 1 month ago
JSON representation
A simple plugin that enables response validation for Fastify
- Host: GitHub
- URL: https://github.com/fastify/fastify-response-validation
- Owner: fastify
- License: mit
- Created: 2020-01-29T13:16:35.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-07T19:12:46.000Z (2 months ago)
- Last Synced: 2025-03-28T10:07:33.307Z (about 2 months ago)
- Topics: ajv, fastify, fastify-plugin, response, validation
- Language: JavaScript
- Homepage: https://npmjs.com/package/@fastify/response-validation
- Size: 115 KB
- Stars: 54
- Watchers: 16
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @fastify/response-validation
[](https://github.com/fastify/fastify-response-validation/actions/workflows/ci.yml)
[](https://www.npmjs.com/package/@fastify/response-validation)
[](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: trueimport 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: trueimport 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)