Introduction: What is Valibot and Why Do You Need It?
\n\nValibot 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\nThe 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).
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\nInstallation
\n\nValibot is distributed via the npm package manager. To install it, run the following command in your project's terminal:
\n\nnpm install valibot\n\nIf you use yarn:
\n\nyarn add valibot\n\nFor pnpm:
\n\npnpm add valibot\n\nAfter installation, you can import the necessary functions into your TypeScript file:
\n\nimport { string, number, object, minLength, maxValue } from 'valibot';\n\nQuick Start with a Code Example
\n\nLet's create a simple validation schema for a user registration form. We'll validate the name, email, and age.
\n\nimport { 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\nIn 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.
Core Methods and Functions
\n\nValibot provides many functions for creating schemas. Let's look at the most important ones.
\n\n1. string()
\nWhat it does: Creates a schema for validating string values. Accepts an optional array of validators for additional checks (length, pattern, etc.).
\nParameters:
\n- \n
validations(optional): an array of validator functions, such asminLength,maxLength,email,url,regex, and others. \n
Example:
\nimport { 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\n2. number()
\nWhat it does: Creates a schema for validating numeric values. Supports c