@fastify/response-validation

中文文档
3.0.5发布于 2026/10/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 5.2万

A simple plugin that enables response validation for Fastify.

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Plugins
  4. Errors
  5. License

@fastify/response-validation

CI NPM version neostandard javascript style

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:

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:

{
  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 configuration, you can do that by using the ajv option:

// 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

// 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:

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:

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.

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

版本与发布记录

@fastify/response-validation 共发布 19 个版本,首次发布于 2022/04/27;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.5latest2026/10/091 个月前
3.0.42026/08/083 个月前
3.0.32025/01/111.8 年前
3.0.22024/11/062 年前
3.0.12024/09/192 年前
3.0.0next2024/09/052 年前

关于 @fastify/response-validation 的常见问题

@fastify/response-validation 是什么?

A simple plugin that enables response validation for Fastify.

如何安装 @fastify/response-validation?

在项目目录运行 npm install @fastify/response-validation,或使用 pnpm add / yarn add / bun add。

@fastify/response-validation 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastify/response-validation 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/response-validation 可以商用吗?

@fastify/response-validation 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/response-validation 的下载量有多少?

根据 npm 官方下载统计,@fastify/response-validation 在 2026/10/15 – 2026/11/13 这 30 天内被下载 52,127 次,日均约 1,738 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/response-validation 还在维护吗?

最新版本 3.0.5 发布于 2026/10/09(1 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/04/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。