Tutorial

Valibot Lightweight Runtime Validation

Adopt Valibot for tree-shakeable schema validation that ships a fraction of Zod bytes while keeping a comparable ergonomic API.

On this page

  1. Introduction
  2. Import only what you use
  3. Compose nested schemas
  4. Handle parse results
  5. Customize error messages
  6. Convert to a type
  7. Verify the bundle
  8. Sources

Introduction

This guide covers lightweight runtime validation with Valibot, a recurring concern in the form-validation scenario. Bundle size matters on the client, and a full schema library can dominate a form widget. Valibot is built so unused methods are dropped by the bundler, and you pay only for the validators you import. The API is close enough to Zod that a port is mostly mechanical.

Import only what you use

Pull named validators like object, string, and number from valibot so the bundler can tree-shake the rest, which is the whole point over a monolithic library.

The trade-off worth naming is that pull named validators like object, string, and number from valibot so the bundler can tree-shake the rest, which is the whole point over a monolithic library adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { object, string, number, array, parse } from 'valibot';
const Order = object({
  id: string(),
  total: number(),
  items: array(object({ sku: string(), qty: number() })),
});
const r = parse(Order, input);

Compose nested schemas

Nest object and array calls to mirror the payload shape, and reuse a sub-schema across fields so an address validates identically wherever it appears.

The trade-off worth naming is that nest object and array calls to mirror the payload shape, and reuse a sub-schema across fields so an address validates identically wherever it appears adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Handle parse results

parse throws on failure, so wrap it or use safeParse to get a typed result and branch without a try-catch polluting the handler.

The trade-off worth naming is that parse throws on failure, so wrap it or use safeparse to get a typed result and branch without a try-catch polluting the handler adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { safeParse } from 'valibot';
const r = safeParse(Order, input);
if (!r.success) {
  for (const issue of r.issues) console.log(issue.path, issue.message);
} else {
  r.output; // typed
}

Customize error messages

Pass a message argument to each validator so the error is user-facing text, not the default English, which keeps i18n in one place.

The trade-off worth naming is that pass a message argument to each validator so the error is user-facing text, not the default english, which keeps i18n in one place adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Convert to a type

Use the Output utility to derive a TypeScript type from the schema so the parsed value flows into handlers without a manual interface that can drift.

The trade-off worth naming is that use the output utility to derive a typescript type from the schema so the parsed value flows into handlers without a manual interface that can drift adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { type Output } from 'valibot';
type OrderT = Output<typeof Order>;

Verify the bundle

Run the production build and inspect the chunk containing Valibot to confirm unused validators are gone, which is the measurable payoff of the migration.

The trade-off worth naming is that run the production build and inspect the chunk containing valibot to confirm unused validators are gone, which is the measurable payoff of the migration adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Valibot docs, bundle phobia.

FAQ

How much smaller is Valibot than Zod on the client?
It depends on how many validators you use, but a typical form ships 3 to 10 times fewer bytes because every method is a standalone export the bundler can drop.
Can I migrate from Zod by changing imports?
Mostly yes; the shape names differ slightly and some methods are named differently, but the object, string, number, array, and parse primitives map over with a find-and-replace plus a type check.

Related Examples