🚀 Documentation crafted by Sonicar Tech LLC — get your own docs shipped in 1 week →
Class Validator Documentation by Sonicar.Tech
Validation

Validating Promises

Validate properties that hold a Promise-returned value with @ValidatePromise(), including combining it with @ValidateNested().

Basic Promise Validation

If your object contains properties with Promise-returned values that should be validated, you need to use the @ValidatePromise() decorator:

import { ValidatePromise, Min } from 'class-validator';

export class Post {
    @Min(0)
    @ValidatePromise()
    userId: Promise<number>;
}

Combining with Nested Validation

It works great with the @ValidateNested decorator:

import { ValidateNested, ValidatePromise } from 'class-validator';

export class Post {
    @ValidateNested()
    @ValidatePromise()
    user: Promise<User>;
}

Async Validation

When validating promises, the validation itself becomes asynchronous:

import { validate } from 'class-validator';

let post = new Post();
post.userId = Promise.resolve(1);

validate(post).then(errors => {
    // handle 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 →

On this page