
class-validator
Validates class properties using TypeScript decorators, for browser and Node.js.
- Stars
- 11.9k
- Forks
- 845
- Licence
- MIT
- Language
- TypeScript
- Last update
- 8 months ago
- Type
- Library
What it is
class-validator lets you attach validation rules to the properties of a class using decorators, such as checking that a string is an email or a number is within a range. You then call validate on an object and get back a list of errors for anything that fails. It also supports non-decorator schemas, and it uses validator.js internally to run the checks.
- Validate a Post class so that a title length, email address, or numeric rating is checked before saving
- Return validation errors over HTTP while hiding the target object with validationError: { target: false }
- Write custom messages with tokens like $value and $property, or with a message function
- Validate each item of an array, set, or map using the each: true option
Great for
- TypeScript developers validating request bodies or form data in Node.js or the browser
- Projects that already model data as classes and want validation rules next to the fields
- Teams building APIs who want to return structured validation errors to clients
Not ideal for
- Projects that do not use TypeScript or decorators, since the main API depends on them (the README also offers a non-decorator option, which is less covered in the excerpt)
- Users who need a full schema language with type inference from one definition, as the README does not compare against such tools
How to get it
- 1
Install with a package manager
You'll need: Node.js · npm 6 or later
Install the package
npm install class-validator --save - 2
Run it
- Install the package with npm install class-validator --save.
- Create a class and add decorators such as @Length, @IsEmail, or @IsInt to its properties.
- Create an instance, set its values, and call validate(post) to get an array of errors.
- Check whether the returned array is empty to decide if the object passed.
- 3
Get the most out of it
- Use validationError: { target: false } when sending errors back over HTTP so the whole object is not exposed.
- Set custom messages with $constraint1 or $value tokens, or pass a function for more detailed messages.
- Use each: true on arrays, sets, or maps to validate every item.
- Use groups in ValidatorOptions to run different rule sets for different situations.
Downloads
Strengths
- Rules are declared next to the fields they check, which keeps models readable
- Returns structured ValidationError objects with property names and constraint messages
- Works in both browser and Node.js environments according to the README
Trade-offs
- The README says forbidUnknownValues defaults to true and advises keeping it; turning it off lets unknown objects pass validation
- Requires npm 6 or later so the dependency tree is flattened correctly
- The README excerpt is truncated, so details of many decorators and advanced features were not reviewed
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| class-validator (this) | 11.9k | Library | beginner | 8 months ago | TypeScript developers validating request bodies or form data in Node.js or the browser |
| zod | 44.2k | Library | beginner | 6 days ago | TypeScript developers who want runtime checks that match their static types |
| yup | 23.7k | Library | beginner | 6 days ago | Developers validating form or API input in JavaScript or TypeScript |
| ajv | 14.9k | Library | intermediate | 2 months ago | Node.js developers validating API request or response bodies |
| validator.js | 23.7k | Library | beginner | 2 months ago | JavaScript developers validating form or API input on Node.js |
| vee-validate | 11.3k | Library | intermediate | 8 months ago | Vue developers who need form validation without writing it from scratch |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.