Data validation in TypeScript simply and safely

β€’
3 min read
Table of Contents

Valibot is a new validation library for JavaScript and TypeScript, designed to be as small as possible, as fast as possible, and fully-typed. Compared to libraries like Zod or Yup, it offers significantly smaller bundle size and extreme performance while maintaining a clear API and strong type checking.

The goal of Valibot is to provide runtime data validation with the lowest possible impact on both performance and the size of the resulting JavaScript bundle. Valibot is written in pure TypeScript and has no dependencies. It’s therefore suitable for frontend, backend, and edge environments.

Basic usage

import { object, string, number, parse } from 'valibot';

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

const data = parse(UserSchema, {
  name: 'Lucie',
  age: 28,
});
  • object({...}) defines the structure of an object
  • string(), number() are primitive validators
  • parse() runs validation and returns typed output or an error
import * as v from 'valibot';

const LoginSchema = v.object({…});

type LoginData = v.InferOutput<typeof LoginSchema>;

function getLoginData(data: unknown): LoginData {
  return v.parse(LoginSchema, data);
}

Advanced examples

Optional values and default values

import { optional, default_ } from 'valibot';

const Schema = object({
  isActive: optional(default_(true, boolean())),
});

Transformations and extra validation

import * as v from 'valibot' // 1.61kb 

const EmailSchema = v.pipe(v.string(), v.email(), v.endsWith('@example.com'));

Enum validation

import { literal, union } from 'valibot';

const Role = union([
  literal('admin'),
  literal('user'),
  literal('guest'),
]);

Array validation

import { array, string, minLength } from 'valibot';

const Tags = array(string([minLength(2)]));

Custom validator

import { custom } from 'valibot';

const EvenNumber = number([
  custom((value) => value % 2 === 0, 'Must be even'),
]);

Safe parsing

Valibot also supports safe parsing using safeParse():

import { safeParse } from 'valibot';

const result = safeParse(UserSchema, { name: 'Anna', age: 'not-a-number' });

if (!result.success) {
  console.log(result.issues); // detailed errors
} else {
  console.log(result.output); // validated data
}

Key library features

  • small: extremely small bundle (less than 1 kB min+gz), tree-shaking
  • fast: optimized for performance in runtime validation
  • type-safe: full integration with TypeScript
  • dependency-free: pure TypeScript, easy integration
  • modular: validators can be composed like building blocks

Summary

Valibot is a next-generation validation library β€” extremely fast, small, and yet type-safe. It’s suitable where bundle size matters (e.g., frontend, mobile web, edge computing), but also on servers in performance-focused environments.