Manual Validation
Use class-validator's non-decorator validation functions directly, and understand synchronous vs. asynchronous validation.
Overview
There are several methods in the Validator that allow performing non-decorator based validation.
Basic Usage
import { isEmpty, isBoolean } from 'class-validator';
isEmpty(value);
isBoolean(value);Available Validation Functions
All validation decorators have corresponding functions that can be used manually:
import {
isEmail,
isLength,
isInt,
min,
max
} from 'class-validator';
// String validation
isEmail('example@email.com'); // true
isLength('text', 2, 10); // true
// Number validation
isInt(123); // true
min(5, 3); // true
max(5, 10); // trueSynchronous vs Asynchronous
Most validation functions are synchronous, but some (like those involving database queries) are asynchronous:
import { validate, validateSync, validateOrReject } from 'class-validator';
// Synchronous
const errors = validateSync(object);
if (errors.length > 0) {
console.log('Validation failed:', errors);
}
// Asynchronous
validate(object).then(errors => {
if (errors.length > 0) {
console.log('Validation failed:', errors);
}
});
// Using validateOrReject
validateOrReject(object).catch(errors => {
console.log('Validation failed:', errors);
});🚀 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 →Validation Decorators Reference
Full reference of class-validator's built-in decorators grouped by category: common, type, number, date, string, array, and object.
FAQ
Answers to common class-validator questions — plain objects vs. class instances, whitelist, async validators, groups, and browser support.