FAQ
Answers to common class-validator questions — plain objects vs. class instances, whitelist, async validators, groups, and browser support.
Can I validate a plain object instead of a class instance?
Not directly. class-validator reads decorator metadata attached to a real class, so
validate() only works on actual class instances — a plain object literal or a JSON.parse()
result has none of that metadata. Convert it first with
class-transformer's plainToInstance(), then
validate the resulting instance.
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
const instance = plainToInstance(User, plainObject);
const errors = await validate(instance);Why does whitelist: true strip out properties I expected to keep?
whitelist strips any property that has no validation decorator at all — not just
properties that fail validation. If a property is intentionally optional or unchecked but should
still be kept, add @Allow() to it instead of leaving it undecorated. See
Whitelisting.
Can I use class-validator without decorators, in plain JavaScript?
Decorators require TypeScript (or Babel with the decorators plugin) with
experimentalDecorators enabled. Schema-based, decorator-free validation existed briefly but
was removed in v0.12 and won't come back. For JavaScript-only projects, call the exported is*
functions manually — see Manual Validation.
Why isn't my async custom validator's result being respected?
validateSync() ignores every asynchronous validator and treats it as passing, regardless of
its real outcome. If a ValidatorConstraint is marked { async: true }, you must call validate()
(which returns a Promise) instead of validateSync().
How do I validate every item in an array?
Pass each: true in the decorator's options — for example @IsEmail({}, { each: true }) on a
string[] property. The decorator then runs once per array element instead of once on the array
as a whole. See Validating Arrays.
Does class-validator work in the browser, or only in Node.js?
Both. validator.js, which class-validator wraps
internally, has no Node-only dependencies, so the same decorators and validate() calls work in
bundled browser code as well as on the server.
How do I turn a ValidationError into a plain error message string?
Each ValidationError has a constraints object keyed by the failing validator's name (for
example constraints.isEmail), whose value is the human-readable message string. Flatten it to
get plain strings:
const messages = errors
.map((e) => Object.values(e.constraints ?? {}))
.flat();Can I reuse one class for both create and update DTOs with different rules?
Yes, with validation groups. Tag the relevant decorators with groups: ['create'], then pass
{ groups: ['create'] } to validate() so only decorators tagged for that group run. See
Validation Groups.
Frequently asked questions
🚀 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 →Manual Validation
Use class-validator's non-decorator validation functions directly, and understand synchronous vs. asynchronous validation.
About Sonicar Tech
Sonicar Tech LLC is a docs-as-code documentation agency for SaaS, B2B, Enterprise, FinTech, and AI companies — first drafts delivered in 1 week.