Configs2026年5月16日·1 分钟阅读

class-validator — Decorator-Based Validation for TypeScript Classes

A validation library that uses TypeScript decorators to define validation rules directly on class properties, deeply integrated with NestJS and TypeORM.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
class-validator Overview
通用 CLI 安装命令
npx tokrepo install 341b28e0-50ff-11f1-9bc6-00163e2b0d79

Introduction

class-validator brings declarative validation to TypeScript by using decorators on class properties. Instead of writing imperative checks scattered across your code, you annotate DTOs once and validate anywhere. It is the default validation layer in NestJS and pairs naturally with class-transformer for request body parsing.

What class-validator Does

  • Validates class instances using 70+ built-in decorator constraints (email, URL, UUID, ranges, arrays)
  • Supports nested object and array validation via @ValidateNested and @Type decorators
  • Provides conditional validation with @ValidateIf for fields that depend on other values
  • Returns structured error objects with property paths, constraint names, and human-readable messages
  • Allows custom validators and async validators for domain-specific business rules

Architecture Overview

class-validator stores validation metadata (decorator definitions) in a global metadata storage keyed by class constructor and property name. When validate() is called, it iterates the target object's properties, looks up registered constraints, and executes each validator function. Failures accumulate into ValidationError objects with nested children for complex object graphs. The library is synchronous by default but supports async validators that return Promises.

Self-Hosting & Configuration

  • Install from npm alongside class-transformer for automatic plain-to-class conversion
  • Enable experimentalDecorators and emitDecoratorMetadata in tsconfig.json
  • Use whitelist: true in validate options to strip unknown properties (security best practice)
  • Configure forbidNonWhitelisted: true to reject payloads with unexpected fields
  • Group validators with @IsString({ groups: ['create'] }) for context-specific validation

Key Features

  • 70+ built-in validators: string formats, numeric ranges, arrays, dates, enums, and more
  • Integrates seamlessly with NestJS ValidationPipe for automatic request validation
  • Custom decorators via registerDecorator() for domain-specific rules
  • Validation groups allow different rules for create vs. update operations
  • Over 11,700 GitHub stars and 10 million weekly npm downloads

Comparison with Similar Tools

  • Zod — schema-first with type inference; class-validator is decorator-first for OOP codebases
  • Joi — schema builder popular with Express; class-validator integrates deeper with TypeScript classes
  • Yup — similar to Joi with TypeScript support; class-validator co-locates rules on the class itself
  • io-ts — runtime type checking via functional codecs; class-validator is imperative and OOP
  • Valibot — modular schema validation; class-validator uses decorators rather than function composition

FAQ

Q: Do I need class-transformer alongside class-validator? A: For NestJS, yes — it converts plain JSON bodies into class instances before validation. For standalone use, you can instantiate classes manually.

Q: Can I validate arrays of objects? A: Yes. Use @ValidateNested({ each: true }) with @Type(() => ItemDto) on an array property.

Q: Does it work without decorators (plain JS)? A: It requires decorator syntax. For non-TypeScript projects, schema-based libraries like Zod or Joi are more appropriate.

Q: How do I write custom async validators? A: Use registerDecorator with validator.validate() returning a Promise. Common use: checking uniqueness against a database.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产