Installation
How to install class-validator with npm, and enable experimentalDecorators in tsconfig.json to start using decorator-based validation.
Requirements
- npm version 6 or higher
- Node.js
Installing the Package
npm install class-validator --saveIt's important to use at least npm@6 when using class-validator. From npm@6 the dependency tree is flattened, which is required by class-validator to function properly.
Basic Setup
Import the necessary decorators:
import { validate } from 'class-validator';Enable decorators in your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true
}
}You're now ready to start using class-validator in your project! Continue to Basic Usage.
🚀 Need documentation like this for your own product?
This site was built by Sonicar Tech LLC — we help SaaS, B2B, Enterprise, FinTech, and AI companies launch professional, docs-as-code documentation 60% faster and cheaper, with first drafts delivered in 1 week.
Visit sonicar.tech →Class Validator
Class-validator is a powerful validation library for TypeScript and JavaScript that allows you to use decorator and non-decorator based validation.
Basic Usage
Create a class with validation decorators, then validate instances with validate(), validateSync(), and validation options like whitelist.