Conditional Validation¶
The conditional validation decorator (@ValidateIf) can be used to ignore the validators on a property when the provided condition function returns false.
Basic Usage¶
import { ValidateIf, IsNotEmpty } from 'class-validator';
export class Post {
otherProperty: string;
@ValidateIf(o => o.otherProperty === 'value')
@IsNotEmpty()
example: string;
}
In this example, the validation rules applied to example won't be run unless the object's otherProperty is "value".
Important Notes¶
- When the condition is false, all validation decorators are ignored
- The condition function takes the object being validated as a parameter
- The condition function must return a boolean
- Multiple
@ValidateIfdecorators can be used on the same property