{"id":14969170,"url":"https://github.com/fastify/fastify-response-validation","last_synced_at":"2025-04-04T11:13:17.129Z","repository":{"id":38071569,"uuid":"236985974","full_name":"fastify/fastify-response-validation","owner":"fastify","description":"A simple plugin that enables response validation for Fastify","archived":false,"fork":false,"pushed_at":"2025-03-07T19:12:46.000Z","size":118,"stargazers_count":54,"open_issues_count":2,"forks_count":17,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-03-28T10:07:33.307Z","etag":null,"topics":["ajv","fastify","fastify-plugin","response","validation"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/response-validation","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2020-01-29T13:16:35.000Z","updated_at":"2025-03-07T19:12:06.000Z","dependencies_parsed_at":"2024-01-02T00:13:36.067Z","dependency_job_id":"ddd6060c-ca10-40a2-9717-aa1a45fdd1bb","html_url":"https://github.com/fastify/fastify-response-validation","commit_stats":{"total_commits":106,"total_committers":23,"mean_commits":4.608695652173913,"dds":0.6132075471698113,"last_synced_commit":"9bb2cd7eb9df578ad32136668bc068a3103a4226"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-response-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-response-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-response-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-response-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-response-validation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247166168,"owners_count":20894654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajv","fastify","fastify-plugin","response","validation"],"created_at":"2024-09-24T13:41:16.259Z","updated_at":"2025-04-04T11:13:17.080Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/response-validation\n\n[![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)\n[![NPM version](https://img.shields.io/npm/v/@fastify/response-validation.svg?style=flat)](https://www.npmjs.com/package/@fastify/response-validation)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nA simple plugin that enables response validation for Fastify.\nThe use of this plugin will slow down your overall performance, so we suggest using it only during development.\n\n## Install\n```\nnpm i @fastify/response-validation\n```\n\n## Usage\nYou just need to register the plugin and you will have response validation enabled:\n```js\nimport fastify from 'fastify'\n\nconst app = fastify()\n\nawait app.register(require('@fastify/response-validation'))\n\napp.route({\n  method: 'GET',\n  path: '/',\n  schema: {\n    response: {\n      '2xx': {\n        type: 'object',\n        properties: {\n          answer: { type: 'number' }\n        }\n      }\n    }\n  },\n  handler: async (req, reply) =\u003e {\n    return { answer: '42' }\n  }\n})\n\napp.inject({\n  method: 'GET',\n  path: '/'\n}, (err, res) =\u003e {\n  if (err) throw err\n  console.log(res.payload)\n})\n```\n\nDifferent 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:\n```js\n{\n  response: {\n    200: {\n      description: 'Description and all status-code based properties are working',\n      content: {\n        'application/json': {\n          schema: {\n            name: { type: 'string' },\n            image: { type: 'string' },\n            address: { type: 'string' }\n          }\n        },\n        'application/vnd.v1+json': {\n          schema: {\n            fullName: { type: 'string' },\n            phone: { type: 'string' }\n          }\n        }\n      }\n    }\n  }\n}\n```\n\nIf you want to override the default [ajv](https://www.npmjs.com/package/ajv) configuration, you can do that by using the `ajv` option:\n```js\n// Default configuration:\n//    coerceTypes: false\n//    useDefaults: true\n//    removeAdditional: true\n//    allErrors: true\n\nimport responseValidator from '@fastify/response-validation'\n\n// ... App setup\n\nawait fastify.register(responseValidator, {\n  ajv: {\n    coerceTypes: true\n  }\n})\n```\n\nYou can also pass in an instance of ajv\n```js\n// Default configuration:\n//    coerceTypes: false\n//    useDefaults: true\n//    removeAdditional: true\n//    allErrors: true\n\nimport responseValidator from '@fastify/response-validation'\nimport Ajv from 'ajv'\n\n// ... App setup\n\nconst ajv = new Ajv()\nawait fastify.register(responseValidator, { ajv })\n```\n\nBy 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`:\n```js\nimport responseValidator from '@fastify/response-validation'\n\n// ... App setup\nawait fastify.register(responseValidator, {\n  responseValidation: false\n})\n```\n\nAlternatively, you can disable a specific route with the same option:\n```js\nfastify.route({\n  method: 'GET',\n  path: '/',\n  responseValidation: false,\n  schema: {\n    response: {\n      '2xx': {\n        type: 'object',\n        properties: {\n          answer: { type: 'number' }\n        }\n      }\n    }\n  },\n  handler: async (req, reply) =\u003e {\n    return { answer: '42' }\n  }\n})\n```\n\n## Plugins\nYou can also extend the functionalities of the ajv instance embedded in this validator by adding new ajv plugins.\n\n```js\nfastify.register(require('fastify-response-validation'), {\n  ajv: {\n    plugins: [\n      require('ajv-formats'),\n      [require('ajv-errors'), { singleError: false }]\n      // Usage: [plugin, pluginOptions] - Plugin with options\n      // Usage: plugin - Plugin without options\n    ]\n  }\n})\n```\n\n## Errors\n\nThe errors emitted by this plugin are:\n\n- `FST_RESPONSE_VALIDATION_FAILED_VALIDATION`: This error is emitted when a response does not conform to the provided schema.\n\n- `FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED`: This error is emitted when there is no JSON schema available to validate the response.\n\n## License\n[MIT](./LICENSE)\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-response-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-response-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-response-validation/lists"}