Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Validation with `class-validator | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 36 of 56

·Typescript

Validation with `class-validator` & Decorators

20 minutes

Course progress64%

Install

npm i class-validator class-transformer reflect-metadata
npm i -D @types/class-validator

Add import 'reflect-metadata'; at the entry point.

DTO with decorators

import { IsInt, IsString, Length, validate } from ;

   {
  ()
  (, )
  name!: ;

  ()
  age!: ;
}

  () {
   dto = .( (), input);
   errors =  (dto);
   (errors.)   ();
   dto;
}
'class-validator'
export
class
CreateUserDto
@IsString
@Length
2
20
string
@IsInt
number
async
function
validateInput
input: any
const
Object
assign
new
CreateUserDto
const
await
validate
if
length
throw
new
Error
'Invalid'
return

Use this in an Express route to enforce runtime validation while keeping compile‑time types.

Info

class-validator works best with class‑based DTOs; keep them separate from ORM entities for clarity.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonExpress Server Typed with TypeScriptContinue to lesson 37Database Access with TypeORMBack to TypeScript Mastery curriculum