Data Validation in TypeScript: Overview of Zod, Yup, and io-ts Libraries
TypeScript is a statically typed language that checks types at compile time. However, at runtime, TypeScript does not perform any checks. This means that data obtained from external sources — API requests, input forms, configuration files — may not match the expected types. To solve this problem, data validation libraries are used, which check the structure and data types at runtime.
In this article, we will look at three popular libraries for data validation in TypeScript: Zod, Yup, and io-ts. You will learn how they work, what their differences are, and how to choose the right one for your project.
Why is Data Validation Needed in TypeScript?
Imagine you are writing an application that receives data from a user via a form:
interface UserInput { name: string; age: number; email: string;}
function processUserInput(input: UserInput) { console.log(input.name.toUpperCase());}In TypeScript, this code will compile even if at runtime input.name turns out to be undefined or a number. This will lead to an error that is difficult to catch. Validation libraries allow you to guarantee that the data matches the expected schema and provide clear error messages.
Zod — A Modern and Fast Library
Zod is a library for declarative validation and type inference. It is built specifically for TypeScript and offers a concise API.
Installation:
npm install zodUsage example:
import { z } from 'zod';
// Define a validation schemaconst UserSchema = z.object({ name: z.string().min(2, "Name must be at least 2 characters"), age: z.number().int().positive("Age must be a positive number"), email: z.string().email("Invalid email"),});
// Infer type from the schematype User = z.infer<typeof UserSchema>;
// Validate dataconst result = UserSchema.safeParse({ name: "Anna", age: 25, email: "anna@example.com",});
if (result.success) { // result.data has type User console.log(result.data.name);} else { // result.error contains detailed error information console.error(result.error.format());}Zod supports complex structures: nested objects, arrays, unions, discriminated unions. It can also transform data (for example, convert strings to numbers).
Yup — A Popular Library from the React Ecosystem
Yup is a schema validation library widely used with Formik in React applications. It has an API similar to Zod but appeared earlier.
Installation:
npm install yupUsage example:
import * as yup from 'yup';
// Define a schemaconst userSchema = yup.object({ name: yup.string().min(2, "Name must be at least 2 characters").required(), age: yup.number().integer().positive().required(), email: yup.string().email("Invalid email").required(),});
// Validation using the validate methoduserSchema.validate({ name: "Ivan", age: 30, email: "ivan@example.com",}).then(validData => console.log(validData)).catch(err => console.error(err.errors));Yup provides convenient methods for form validation, supports custom error messages, and conditional rules (when). However, it is less tightly integrated with TypeScript than Zod — type inference requires additional steps.
io-ts — A Functional Approach to Validation
io-ts is a library based on functional programming. It uses the concept of codecs for transform