Valibot for TypeScript: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: What is Valibot and Why Do You Need It?

\n\n

Valibot is a modern, modular, and lightweight data validation library for TypeScript and JavaScript. Unlike giants such as Zod or Joi, Valibot is designed with a focus on minimal bundle size and tree-shaking (the ability to remove unused code).

\n\n

The core philosophy of Valibot is "pay only for what you use." If you only need to check a string for a minimum length, you import only string and minLength, not the entire library. This makes Valibot an ideal choice for projects where the size of the final JavaScript bundle matters (e.g., for mobile applications or component libraries).

\n\n

The library is fully typed and provides excellent integration with TypeScript, automatically inferring types from validation schemas. Valibot supports both simple primitives (strings, numbers, booleans) and complex structures (objects, arrays, union types, recursive schemas).

\n\n

Installation

\n\n

Valibot is distributed via the npm package manager. To install it, run the following command in your project's terminal:

\n\n
npm install valibot
\n\n

If you use yarn:

\n\n
yarn add valibot
\n\n

For pnpm:

\n\n
pnpm add valibot
\n\n

After installation, you can import the necessary functions into your TypeScript file:

\n\n
import { string, number, object, minLength, maxValue } from 'valibot';
\n\n

Quick Start with a Code Example

\n\n

Let's create a simple validation schema for a user registration form. We'll validate the name, email, and age.

\n\n
import { object, string, number, email, minLength, maxValue, safeParse } from 'valibot';\n\n// 1. Define the schema\nconst UserSchema = object({\n  name: string([minLength(2, 'Name must be at least 2 characters')]),\n  email: string([email('Invalid email')]),\n  age: number([maxValue(120, 'Age cannot be greater than 120')])\n});\n\n// 2. Data to validate\nconst inputData = {\n  name: 'Anna',\n  email: 'anna@example.com',\n  age: 30\n};\n\n// 3. Validate\nconst result = safeParse(UserSchema, inputData);\n\nif (result.success) {\n  console.log('Data is valid:', result.output);\n  // result.output has type { name: string; email: string; age: number; }\n} else {\n  console.error('Validation errors:', result.issues);\n  // result.issues is an array of objects describing errors\n}
\n\n

In this example, we used safeParse — a safe method that does not throw an exception but returns an object with a success field. If validation succeeds, result.output will contain the validated data with correct types.

\n\n

Core Methods and Functions

\n\n

Valibot provides many functions for creating schemas. Let's look at the most important ones.

\n\n

1. string()

\n

What it does: Creates a schema for validating string values. Accepts an optional array of validators for additional checks (length, pattern, etc.).

\n

Parameters:

\n
    \n
  • validations (optional): an array of validator functions, such as minLength, maxLength, email, url, regex, and others.
  • \n
\n

Example:

\n
import { string, minLength, maxLength, email } from 'valibot';\n\nconst UsernameSchema = string([minLength(3), maxLength(20)]);\nconst EmailSchema = string([email()]);\n\nconsole.log(safeParse(UsernameSchema, 'Jo')); // { success: false, issues: [...] }\nconsole.log(safeParse(EmailSchema, 'test@test.com')); // { success: true, output: 'test@test.com' }
\n\n

2. number()

\n

What it does: Creates a schema for validating numeric values. Supports c

Recommendations