API Reference
class-validator
Decorator-based validation for TypeScript classes, with a global validate function and rich constraints.
Official Documentation ↗CompatibilityThis documentation covers version
v0.14. Check the official docs for the latest API surface.Installation
npm
npm install class-validatorMethods
No methods match this filter.
class-validator attaches metadata via decorators and validates class instances at runtime.
Key Features
- Decorators for common constraints
- Nested and array validation
- Custom validators and messages
Usage Notes
Pin a version and check the official docs for the API surface you depend on before upgrading.
Errors
The following points are quoted verbatim from the official documentation (https://github.com/typestack/class-validator). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Validation errors The validate method returns an array of ValidationError objects. (https://github.com/typestack/class-validator)
- Validation messages You can specify validation message in the decorator options and that message will be returned in the ValidationError returned by the validate method (in the case that validation for this field fails). (https://github.com/typestack/class-validator)
- You can also supply a validation constraint name - this name will be used as “error type” in ValidationError. (https://github.com/typestack/class-validator)
FAQ
- Does it work without decorators?
- Experiments are experimental; decorators are the supported path. Enable experimentalDecorators and emitDecoratorMetadata in tsconfig.
- How do I validate nested objects?
- Annotate the property with @ValidateNested() and @Type(() => Child), otherwise the nested instance is not traversed.
- How do I strip unknown fields?
- Pass whitelist: true and forbidNonWhitelisted: true to validate options so extra properties are rejected or removed.
Report a problem with this API docOpens a pre-filled issue on GitHub.