fastify-no-response-validation
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

fastify-no-response-validation

NPM version NPM downloads CI Coverage

Disable response validation in Fastify.

Why This Plugin?

By default, Fastify doesn't validate responses unless you're using the @fastify/response-validation plugin, which only supports AJV (Fastify's default validator). However, when working with Zod schemas, especially through fastify-zod-openapi or fastify-type-provider-zod, response validation is automatically enabled.

This plugin disables response validation in Fastify. After applying this plugin:

  1. Responses that don't match the defined schema won't trigger a 500 error
  2. API performance improves as response validation overhead is removed
  3. Schema documentation is preserved for API documentation purposes

Pro Tip

When using this plugin, you don't need to include fastify.setSerializerCompiler(serializerCompiler) in your setup. The plugin takes care of serialization internally.

Install

npm install fastify-no-response-validation

Usage

import Fastify from 'fastify';
import fastifyNoResponseValidation from 'fastify-no-response-validation';
import { validatorCompiler, type FastifyZodOpenApiTypeProvider } from 'fastify-zod-openapi';
import { z } from 'zod';

const fastify = Fastify().withTypeProvider<FastifyZodOpenApiTypeProvider>();

// Set up Zod validation
fastify.setValidatorCompiler(validatorCompiler);

// Register the plugin to disable response validation
fastify.register(fastifyNoResponseValidation);

// Define a route with a schema
fastify.get('/user', {
  schema: {
    response: {
      200: z.object({
        id: z.number(),
        name: z.string(),
      }),
    },
  },
}, (request, reply) => {
  // This response doesn't match the schema, but it will still be sent
  return { name: 'John', age: 30 } as any;
});

fastify.listen({ port: 3000 }, (err) => {
  if (err) throw err;
  console.log('Server is running on http://localhost:3000');
  console.log('Try: http://localhost:3000/user');
});

Compatibility

This plugin is compatible with Fastify v4.x and v5.x.

Contributing

Contributions, issues, and feature requests are welcome!

License

This project is licensed under the MIT License.

Package Sidebar

Install

npm i fastify-no-response-validation

Weekly Downloads

7

Version

1.0.2

License

MIT

Unpacked Size

5.68 kB

Total Files

6

Last publish

Collaborators

  • jsnimda