Validation Groups¶
Overview¶
In different situations you may want to use different validation schemas for the same object. In such cases you can use validation groups.
Basic Usage¶
import { validate, Min, Length } from 'class-validator';
export class User {
@Min(12, {
groups: ['registration']
})
age: number;
@Length(2, 20, {
groups: ['registration', 'admin']
})
name: string;
}
let user = new User();
user.age = 10;
user.name = 'Alex';
validate(user, {
groups: ['registration']
}); // this will not pass validation
validate(user, {
groups: ['admin']
}); // this will pass validation
validate(user, {
groups: ['registration', 'admin']
}); // this will not pass validation
validate(user, {
groups: undefined // the default
}); // this will not pass validation
Important Notes¶
- The
always: trueflag in validation options means the validation must be applied regardless of groups - Multiple groups can be specified for a single decorator
- If no groups are specified, the default group is used
- Groups can be used to create different validation scenarios for the same object