Validating Promises¶
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 than building an in-house team, with first drafts delivered in 1 week.