33 lines
992 B
TypeScript
33 lines
992 B
TypeScript
|
import {
|
||
|
registerDecorator,
|
||
|
ValidationOptions,
|
||
|
ValidationArguments,
|
||
|
} from 'class-validator';
|
||
|
|
||
|
export function HasTruthyWith(
|
||
|
property: string,
|
||
|
validationOptions?: ValidationOptions,
|
||
|
) {
|
||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||
|
return function (object: Object, propertyName: string) {
|
||
|
registerDecorator({
|
||
|
name: 'hasTruthyWith',
|
||
|
target: object.constructor,
|
||
|
propertyName: propertyName,
|
||
|
constraints: [property],
|
||
|
options: validationOptions,
|
||
|
validator: {
|
||
|
validate(value: any, args: ValidationArguments) {
|
||
|
const [relatedPropertyName] = args.constraints;
|
||
|
const relatedValue = (args.object as any)[relatedPropertyName];
|
||
|
return (
|
||
|
typeof value === 'boolean' &&
|
||
|
typeof relatedValue === 'boolean' &&
|
||
|
(value || relatedValue)
|
||
|
); // you can return a Promise<boolean> here as well, if you want to make async validation
|
||
|
},
|
||
|
},
|
||
|
});
|
||
|
};
|
||
|
}
|