class-validator
Decorator-based validation for TypeScript/JavaScript classes. Add validation decorators (@IsString(), @IsEmail(), @MinLength(5)) to class properties and call validate(instance) to check validity. Works with NestJS DTOs (Data Transfer Objects) as the standard validation approach. Built on validator.js for the underlying validators. Part of the typestack ecosystem alongside class-transformer.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
MIT licensed. Input validation reduces attack surface. Whitelist validation (whitelist: true in NestJS) prevents over-posting attacks. Decorator metadata requires reflect-metadata polyfill.
⚡ Reliability
Best When
You're building NestJS applications where class-validator is the de-facto standard for DTO validation with NestJS's built-in ValidationPipe.
Avoid When
You're not using NestJS or TypeScript decorators — the decorator-based approach has unnecessary boilerplate outside NestJS context.
Use Cases
- • Validate NestJS DTO classes — the standard approach for request body validation in NestJS applications
- • Define typed data contracts for agent API endpoints with inline validation rules as class decorators
- • Combine with class-transformer to validate and transform plain JSON objects into validated class instances
- • Validate TypeScript domain model classes at runtime using the same decorator definitions used for type checking
- • Implement layered validation with groups (@IsNotEmpty({ groups: ['create'] })) for create vs update scenarios
Not For
- • Non-NestJS or non-decorator TypeScript projects — Zod or Joi provide better DX without TypeScript decorator requirements
- • Plain JavaScript — class-validator requires TypeScript with experimentalDecorators enabled
- • Simple one-off validations — decorator-based approach requires class definitions; use Zod for ad-hoc schema validation
Interface
Authentication
Local library — no authentication required. MIT licensed.
Pricing
MIT licensed. Zero cost.
Agent Metadata
Known Gotchas
- ⚠ Requires tsconfig.json: experimentalDecorators: true and emitDecoratorMetadata: true — without these, decorators silently do nothing
- ⚠ Must import 'reflect-metadata' at application entry point: import 'reflect-metadata' — required for decorator metadata to work
- ⚠ validate() is ASYNC — returns Promise<ValidationError[]>; use validateSync() for synchronous validation but it has limitations
- ⚠ Plain objects are NOT validated — must use class-transformer's plainToInstance() to convert JSON to class instances before validation: const dto = plainToInstance(CreateUserDto, reqBody); validate(dto)
- ⚠ Nested object validation requires @ValidateNested() decorator AND class-transformer's @Type() decorator: @ValidateNested() @Type(() => AddressDto) address: AddressDto
- ⚠ forbidUnknownValues: true is recommended — without it, unknown properties in the class are silently ignored; set in ValidationPipe options for NestJS
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for class-validator.
Scores are editorial opinions as of 2026-03-06.