class-validator: what its ValidatorOptions actually change when you validate a class
Decorator-based property validation for classes.
At a glance
- What is it?
- class-validator turns decorated properties into runtime checks backed by validator.js and hands you an array of ValidationError objects. The interesting part is not the decorators but the options object: several of its flags silently change whether a failing payload reports an error, an empty result, or nothing at all.
- Who is it for?
- It fits projects that already describe payloads as decorated classes and want validation to live next to that description. It does not fit codebases that want schema defined once and reused as types, since the rules are mainly expressed as decorators on classes.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Activity is slowing. The repository last received commits 8 months ago.
- What is it written in?
- Mainly TypeScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.
Star history
- typestack/class-validator11,855 ★ · +16
| Repository | 2026-09-22 | 2026-11-25 |
|---|---|---|
| typestack/class-validator | 11.8k | 11.9k |
16 Stars in the last 64 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Decorators record metadata, and validator.js does the actual checking
Two things happen when a decorator runs. It registers metadata against the property, and at validation time the recorded rules are handed to validator.js, which is what actually decides whether a value passes. That division matters, because the quality and the semantics of every rule you write are bounded by a third-party rule set rather than by this library. The dependency is declared as validator ^13.15.22, with libphonenumber-js ^1.11.1 also present for phone number handling. The package is not decorator-only: it allows non-decorator based validation, and one documentation section is devoted to defining a validation schema without decorators. What the library never does is decide when to run or what to do with the result, since validate hands back the error array and the caller decides whether a failure blocks the request, rejects it, or is merely logged.
forbidUnknownValues defaults to true, and turning it off passes everything
The most consequential entry in the options interface is two words long. forbidUnknownValues is set to true by default, and the documentation marks that default as highly advised to keep, because setting it to false will result in unknown objects passing the validation. Passing is the failure mode to understand precisely. It is not that a rule stops working; it is that the whole call reports success for an object the library cannot reason about. Anything without the metadata the decorators install falls into that category: a plain object built from parsed JSON, or an instance of a class you forgot to decorate. The practical effect of a careless refactor is that a validated DTO silently becomes an unchecked literal, and no error appears to tell you. Keep the default, and if you disable it deliberately, treat that call as having no validation at all.
skipMissingProperties decides whether an absent field counts as a failure
One boolean changes the meaning of a missing value. With skipMissingProperties set, a property that is undefined or null passes; without it, that same property counts as failed. This is where partial and full submissions diverge, and the trap is that the same decorated class serves both. A create endpoint validating a full body and a patch endpoint validating only the changed fields need opposite settings for the identical DTO, and choosing one default makes one of them wrong. Two neighbouring options shape the output as well. stopAtFirstError short-circuits collection, which is convenient for form feedback and ruinous for an API that returns every problem. dismissDefaultMessages removes the built-in strings, leaving you to supply every message yourself.
ValidationError ships the whole input object unless you ask it not to
Every failure comes back as a ValidationError, and the shape is worth reading because of what it carries by default. The fields are target, the object that was validated, property, value, an optional constraints map holding messages keyed by rule type, and optional children containing the nested errors of that property. Since target is the entire input object, returning this array from an HTTP handler sends the whole record back to the client, including fields that had nothing to do with the failure. The remedy is one option, validationError: { target: false }, which the documentation recommends specifically for that case, and there is a matching value flag for the same reason. Keep both on where the object is useful to you, for logging and debugging, and turn them off at the serialization boundary. Check nested objects separately, since children entries are ValidationError objects of the same shape.
Whitelisting mutates your input object rather than rejecting it
Whitelisting exists because decorators describe the properties you know about while payloads carry the properties somebody else sent. With whitelist set to true, properties that have no validation decorator are removed from the object. That is the behavior to internalize before enabling it: this is not rejection, it is mutation, so the object you passed in is not the object you hold afterwards, and any other reference to it sees the pruned shape. Pair it with forbidNonWhitelisted and an unknown key becomes an error instead of a silent deletion, which is the combination you want on a public API, since it makes the accepted key set exactly the decorated set. The cost lands on your workflow: a decorator list becomes a contract, and adding a field to a payload without adding its decorator either drops the value or fails the request, so the flags and your DTO definitions have to change together.
Groups and inheritance let one class serve several different callers
A single class often has to satisfy more than one caller, and a groups array in ValidatorOptions selects which decorated rules apply to the current pass, so a create path and an update path can share one class while each sees only the rules it should. Conditional validation covers the related case where a rule applies only when a sibling field matches, and the inheritance section covers child classes that need the parent's decorators carried forward. The sample directory ships runnable examples named for exactly these cases, including using-groups, inheritance-support, nested-objects, schemas and es6-maps. What none of this enforces is spelling. A group name that does not match between the decorator and the call site is not an error; it simply selects no rules, and the pass succeeds because nothing was checked. Keep group names in shared constants instead of repeating string literals.
Four entry points ship, but the default build command makes only one
The published package declares four ways in: main at ./cjs/index.js, module at ./esm5/index.js, es2015 at ./esm2015/index.js, and typings at ./types/index.d.ts, with sideEffects set to false so a bundler can drop imports it cannot see used. The scripts show how uneven that is:
"build": "npm run build:cjs",
"build:es2015": "tsc --project tsconfig.prod.esm2015.json",
"build:esm5": "tsc --project tsconfig.prod.esm5.json",
"build:types": "tsc --project tsconfig.prod.types.json",
"build:umd": "rollup --config rollup.config.js"The default build is CommonJS only, and the ES5, ES2015, type and UMD artifacts are separate steps nothing chains for you. Consequence for a consumer: the output your app receives depends on which field your bundler resolves rather than on what you requested. Consequence for a fork: running the build alone leaves the ESM and UMD entry points stale while your CommonJS copy changes, which produces the confusing case where two consumers of the same fork behave differently.
Version 0.15.1, and the last push landed on 2026-03-25
Package.json reports version 0.15.1, matching the release feed: v0.15.1 and v0.15.0 were both published on 2026-02-26, and v0.14.4 the day before. The repository's last push is dated 2026-03-25, and its default branch is develop rather than a release branch. Two consequences follow for anyone planning to depend on this. The package is still on a 0.x line, which carries no stability guarantee across minor bumps, so an upgrade from 0.14 to 0.15 deserves the same reading of the changelog as a major upgrade. And with the newest commit in March, there is no recent stream of fixes to assume you are picking up when you install. Pin the exact version, upgrade on purpose, and confirm which branch you cloned, since develop is what a fresh clone gives you and it may not be what you are running in production.
Editorial conclusion
It fits projects that already describe payloads as decorated classes and want validation to live next to that description. It does not fit codebases that want schema defined once and reused as types, since the rules are mainly expressed as decorators on classes. Before you wire it into a public endpoint, decide the forbidUnknownValues and whitelist settings explicitly, turn off the target field so errors can be serialized, pin an exact 0.x version, and read the changelog before upgrading.
Frequently asked questions
What is a class-validator?
It is a TypeScript library for decorator-based property validation on classes that also supports non-decorator based validation. Internally it uses validator.js to perform the checks, and it runs on both browser and Node.js platforms.
how to install class validator
Run npm install class-validator --save. The documentation adds that you should use at least npm@6, because from npm@6 the dependency tree is flattened, which the library requires in order to function properly.
how to use class validator
Decorate the properties you want checked, then call validate on the instance. The call optionally takes a ValidatorOptions object as a second parameter and returns an array of ValidationError objects, each holding target, property, value, constraints and children.
is email class validator
Yes, @IsEmail() is one of the decorators, used alongside @Length, @Contains, @IsInt, @Min, @Max, @IsFQDN and @IsDate in the documented example. Because validation is delegated to validator.js, the email rule follows that library's behavior.
how to use class validator in nestjs
The usage shown in the README does not depend on a framework: decorate the properties, call validate with an optional ValidatorOptions object, and read the ValidationError array it returns.
class validator is uuid
Each rule is a decorator backed by validator.js, with optional arguments such as the minimum and maximum used by @Length; the README has a Validation decorators section for the full list. The validator dependency is declared as ^13.15.22.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/typestack-class-validator)Star history chart for your README
[](https://hysenlabs.com/projects/typestack-class-validator)Was this useful?