npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastify-valibot-tools

v1.1.1

Published

Tools to integrate valibot with fastify

Readme

fastify-valibot-tools

Utilities for using Valibot schemas with Fastify validation, response serialization, and OpenAPI documentation.

Features

  • Validates request bodies, query strings, route parameters, and headers with Valibot.
  • Returns transformed Valibot output from request validation.
  • Infers Fastify request and reply types from route schemas.
  • Serializes responses with fast-json-stringify.
  • Converts Valibot schemas to OpenAPI 3.0 schemas.
  • Generates OpenAPI documentation through @fastify/swagger.
  • Supports Fastify content-type-specific body and response schemas.

Installation

npm install fastify-valibot-tools fastify valibot

The package is ESM-only and requires Fastify 5.12.7 or newer within the Fastify 5 release line.

Validation and serialization

Register ValibotValidationPlugin before declaring routes that use Valibot schemas:

import Fastify from 'fastify';
import { ValibotValidationPlugin } from 'fastify-valibot-tools';
import * as v from 'valibot';

const fastify = Fastify();

await fastify.register(ValibotValidationPlugin);

const CreateUserSchema = v.object({
	name: v.pipe(v.string(), v.minLength(1)),
	age: v.pipe(v.number(), v.minValue(18))
});

const UserSchema = v.object({
	id: v.number(),
	name: v.string(),
	age: v.number()
});

fastify.post(
	'/users',
	{
		schema: {
			body: CreateUserSchema,
			response: {
				201: UserSchema
			}
		}
	},
	async (request, reply) => {
		const user = {
			id: 1,
			name: request.body.name,
			age: request.body.age
		};

		return reply.code(201).send(user);
	}
);

await fastify.listen({ port: 3000 });

The plugin augments Fastify's default type provider. Request values are inferred as v.InferOutput<Schema> because Valibot validation and transformations run before the handler. Reply values are inferred as v.InferInput<Schema> because response schemas are used for serialization and are not parsed by Valibot.

Validation errors

Invalid requests receive Fastify's standard HTTP 400 validation response. The underlying Valibot issues are available on ValibotValidationError.issues:

import Fastify from 'fastify';
import ValibotValidationPlugin, {
	ValibotValidationError
} from 'fastify-valibot-tools/validation-plugin';

const fastify = Fastify();

await fastify.register(ValibotValidationPlugin);

fastify.setErrorHandler((error, request, reply) => {
	if (error instanceof ValibotValidationError) {
		return reply.code(400).send({
			message: error.message,
			issues: error.issues
		});
	}

	return reply.send(error);
});

Loose objects

Additional properties from v.looseObject() response schemas are preserved during serialization:

const ResponseSchema = v.looseObject({
	id: v.number()
});

fastify.get(
	'/user',
	{
		schema: {
			response: {
				200: ResponseSchema
			}
		}
	},
	async () => ({ id: 1, displayName: 'John' })
);

OpenAPI documentation

ValibotSwaggerPlugin registers @fastify/swagger in dynamic OpenAPI mode and converts route schemas automatically. Register it before declaring routes:

import Fastify from 'fastify';
import {
	ValibotSwaggerPlugin,
	ValibotValidationPlugin
} from 'fastify-valibot-tools';
import * as v from 'valibot';

const fastify = Fastify();

await fastify.register(ValibotValidationPlugin);
await fastify.register(ValibotSwaggerPlugin, {
	info: {
		title: 'Users API',
		version: '1.0.0'
	}
});

fastify.get(
	'/users/:id',
	{
		schema: {
			summary: 'Get a user',
			params: v.object({
				id: v.string()
			}),
			response: {
				200: v.object({
					id: v.string(),
					name: v.string()
				})
			}
		}
	},
	async request => ({
		id: request.params.id,
		name: 'John'
	})
);

await fastify.ready();

const openapiDocument = fastify.swagger();

The generated document uses OpenAPI 3.0.3. This plugin generates the document but does not expose Swagger UI.

Common responses

Use addCommonResponses to add Valibot response schemas based on the Fastify route. The callback uses numeric HTTP status-code keys, which the plugin normalizes to strings for OpenAPI. Route-specific responses take precedence when they use the same status code.

await fastify.register(ValibotSwaggerPlugin, {
	info: {
		title: 'Users API',
		version: '1.0.0'
	},
	addCommonResponses: route => ({
		401: v.object({
			message: v.string()
		}),
		...(route.method === 'DELETE'
			? {
					404: v.object({
						message: v.string()
					})
				}
			: {})
	})
});

Common response schemas and Valibot schemas declared directly on routes are converted automatically.

Content-type-specific schemas

Fastify content wrappers are supported for request bodies and responses. The nested schema values should be Valibot schemas:

fastify.post('/users', {
	schema: {
		body: {
			content: {
				'application/json': {
					schema: CreateUserSchema
				}
			}
		},
		response: {
			201: {
				description: 'Created user',
				content: {
					'application/json': {
						schema: UserSchema
					}
				}
			}
		}
	},
	handler: async () => ({
		id: 1,
		name: 'John',
		age: 30
	})
});

Converting schemas directly

Use valibotToJsonSchema when an OpenAPI 3.0 schema is needed outside the plugins:

import { valibotToJsonSchema } from 'fastify-valibot-tools';
import * as v from 'valibot';

const schema = v.object({
	id: v.number(),
	name: v.string()
});

const inputSchema = valibotToJsonSchema(schema, 'input');
const outputSchema = valibotToJsonSchema(schema, 'output');

The second argument selects which side of a Valibot transformation pipeline is represented. Unsupported Valibot features are ignored during conversion, so review the generated schema when using custom or non-JSON-compatible schemas.

Package exports

The following entry points are available:

import {
	ValibotSwaggerPlugin,
	ValibotValidationPlugin,
	valibotToJsonSchema
} from 'fastify-valibot-tools';

import valibotToJsonSchema from 'fastify-valibot-tools/to-json-schema';
import ValibotValidationPlugin from 'fastify-valibot-tools/validation-plugin';
import ValibotSwaggerPlugin from 'fastify-valibot-tools/swagger-plugin';

License

MIT