How to Validate with Valibot
Use Valibot as a lightweight schema library for tree-shakeable validation, so the bundle stays small when only a few checks run on the client.
On this page
Introduction
The hardest part of validate with a tiny bundle is not writing the code — it is deciding what the code is allowed to reject. A Valibot setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in Valibot, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.
Valibot earns its place when validate with a tiny bundle is the bottleneck the user feels. Before that, it is overhead that slows the common case to protect a rare one. Measure the rejection rate and the tail latency, then decide whether the guard is worth its cost. A guard that never fires is a guard you can remove; a guard that fires constantly is a signal that the producer contract is wrong, not that the guard is too strict.
The sections below walk through the concrete decisions: where to put the valibot check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.
Define a pipe schema
Define a pipe schema is the first decision because it determines what the rest of the Valibot pipeline can assume. A wrong choice here turns into validate with a tiny bundle under load, and the invariant you thought Valibot guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Define a pipe schema is the one most teams skip: drive valibot to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
import * as v from 'valibot';
const Schema = v.pipe(v.string(), v.minLength(2), v.maxLength(20));
const r = v.safeParse(Schema, input);
if (!r.success) console.log(r.issues);
The typescript above is the shape to ship for validate with a tiny bundle. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.
Run parse and safeParse
The mechanism behind Run parse and safeParse is straightforward in Valibot, but the consequences ripple. A wrong choice here turns into validate with a tiny bundle under load, and the invariant you thought Valibot guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Run parse and safeParse is the one most teams skip: drive valibot to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Tree-shake unused actions
Tree-shake unused actions is where most Valibot implementations leak performance. A wrong choice here turns into validate with a tiny bundle under load, and the invariant you thought Valibot guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Tree-shake unused actions is the one most teams skip: drive valibot to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
test('rejects too short', () => {
expect(v.safeParse(Schema, 'a').success).toBe(false);
});
Pin the behavior with this test. A Valibot setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.
Handle issue codes
The hard part of Handle issue codes is not the happy path — it is what happens when valibot fails halfway. A wrong choice here turns into validate with a tiny bundle under load, and the invariant you thought Valibot guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Handle issue codes is the one most teams skip: drive valibot to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Migrate from Zod gradually
Migrate from Zod gradually should be observable without a debugger. A wrong choice here turns into validate with a tiny bundle under load, and the invariant you thought Valibot guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Migrate from Zod gradually is the one most teams skip: drive valibot to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Test the bundle size
Test the bundle size is a trade, not a winner. A wrong choice here turns into validate with a tiny bundle under load, and the invariant you thought Valibot guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.
The test for Test the bundle size is the one most teams skip: drive valibot to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.
Common pitfalls
The common pitfalls in validate with a tiny bundle all reduce to one mistake: treating Valibot as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.
- Skipping the valibot boundary so validate with a tiny bundle enters unvalidated.
- Assuming Valibot is fast without measuring the tail latency under load.
- Letting validate with a tiny bundle fail silently instead of failing loudly with a metric.
- Testing only the happy path so the valibot failure surfaces in production.
- Sharing mutable state across requests so one bad input poisons the next.
Conclusion
Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: Valibot fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.
Self-check: write a test that drives valibot to the failing condition and assert the system degrades gracefully. Then validate with a tiny bundle is a property you maintain, not a feature you hope for.
Sources: Valibot
FAQ
- Why Valibot over Zod?
- Smaller bundle when tree-shaken. If you only use a few actions on the client, Valibot ships less code.
- Can I share schemas with the server?
- Yes. The schema is plain data, so import the same module on both sides.
Report a problem with this articleOpens a pre-filled issue on GitHub.