class-validator
By the TypeStack open-source team
class-validator is a TypeScript library that adds validation rules to class properties using decorators, letting developers describe constraints such as required fields, length limits, or email formats directly on a data class rather than…
Definition
class-validator is a TypeScript library that adds validation rules to class properties using decorators, letting developers describe constraints such as required fields, length limits, or email formats directly on a data class rather than in a separate schema object. It is most commonly used with NestJS and other TypeScript back-end frameworks to validate incoming request data mapped onto Data Transfer Object classes. Validation is performed by calling functions that inspect an object's decorators at runtime and return a list of constraint violations.
Overview
class-validator addresses the pattern common in TypeScript back ends where request data is deserialized into a class instance, often called a Data Transfer Object or DTO, and then needs to be checked against business rules before use. Rather than defining validation rules in a schema object separate from the class definition, class-validator lets developers annotate the class's own properties directly, keeping the shape of the data and its validation rules in the same place. Mechanically, class-validator uses TypeScript decorators, functions like `@IsString()`, `@IsEmail()`, `@MinLength(5)`, or `@IsOptional()`, attached above each class property, which register metadata about that property's constraints using the `reflect-metadata` library. At validation time, calling `validate()` on an instance of the class reads that metadata, checks the actual property values against it, and returns an array of validation error objects describing which properties failed which constraints. It also supports nested validation, where a property that is itself a class instance can have its own decorated constraints checked recursively. class-validator's decorator-based, class-centric approach distinguishes it from schema-object validators like Joi, Zod, or Yup, which define validation rules as standalone schema values rather than attaching them to a class. This makes class-validator a natural fit for frameworks like NestJS that already lean on classes and decorators throughout, such as for dependency injection and route definitions, since validation decorators follow the same idiom. It is commonly paired with `class-transformer`, which handles converting plain JSON objects into class instances before validation runs. In practice, class-validator is used almost synonymously with NestJS request validation, applied through NestJS's `ValidationPipe` to automatically validate incoming request bodies against DTO classes without extra boilerplate in each route handler. It also sees use in other TypeScript codebases that model data as classes and want inline validation without maintaining a parallel schema definition. The main trade-offs are a tighter coupling to TypeScript's decorator and class conventions, which does not translate well to plain JavaScript or functional codebases, and a dependency on `reflect-metadata` for the metadata reflection that powers decorators. Teams not already using classes and decorators as their primary data-modeling style, or working in a framework that favors schema-first validation, often find libraries like Zod a more natural fit, since schema objects can be defined and inferred from without any decorator or reflection machinery. Decorator metadata reflection can also complicate certain build setups, such as bundling for edge runtimes, since it relies on TypeScript's experimental decorator support and the reflect-metadata polyfill being configured correctly across the whole build pipeline.
Key Features
- Decorator-based validation rules attached directly to class properties
- Deep integration with NestJS's dependency-injection and DTO conventions
- Supports nested validation of class properties that are themselves classes
- Commonly paired with class-transformer to convert plain objects to class instances
- Wide library of built-in decorators for common constraints like email and length
- Returns structured validation error arrays describing failed constraints
Use Cases
Alternatives
Frequently Asked Questions
From the Blog
Why your Keras model predicts one class for everything
A model that outputs the majority class for every input has collapsed to the prior, and there are five likely causes. Check the confusion matrix first, then class balance, the activation and loss pairing, input scaling, the learning rate, and label alignment — in that order.
Read More ProgrammingReact Hooks Explained: useState, useEffect, and Beyond
React Hooks replaced class components and changed how React developers think about state and side effects. This guide explains useState, useEffect, useContext, useRef, and custom hooks clearly, with practical examples for each.
Read More ProgrammingPython Decorators: A Practical Guide for Beginners
Decorators are one of Python's most powerful features — they let you wrap functions with reusable logic without modifying the original. This guide explains how they work from first principles, builds several practical decorators (timing, caching, authentication), and covers class-based decorators and decorator factories.
Read More AI & TechnologyWhat Is a Confusion Matrix in Machine Learning
A confusion matrix is a simple table that shows exactly where a classification model gets predictions right and wrong, broken down by every class.
Read More