44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import {
|
|
ValidateBy,
|
|
ValidationArguments,
|
|
ValidationOptions,
|
|
buildMessage,
|
|
} from 'class-validator';
|
|
// TODO refactor ??
|
|
// TODO propely set driver max limit
|
|
export function hasProperDriverSeats(value: any, args: ValidationArguments) {
|
|
console.log('here ');
|
|
console.log(value);
|
|
console.log(args.object);
|
|
if (value === true && typeof args.object['seatsDriver'] === 'number')
|
|
return args.object['seatsDriver'] > 0;
|
|
else if (
|
|
(value === false || value === null || value === undefined) &&
|
|
(args.object['seatsDriver'] === 0 ||
|
|
args.object['seatsDriver'] === null ||
|
|
args.object['seatsDriver'] === undefined)
|
|
)
|
|
return true;
|
|
else return false;
|
|
}
|
|
|
|
export function HasProperDriverSeats(
|
|
validationOptions?: ValidationOptions,
|
|
): PropertyDecorator {
|
|
return ValidateBy(
|
|
{
|
|
name: '',
|
|
constraints: [],
|
|
validator: {
|
|
validate: (value, args: ValidationArguments): boolean =>
|
|
hasProperDriverSeats(value, args),
|
|
defaultMessage: buildMessage(
|
|
() => `driver and driver seats are not correct`,
|
|
validationOptions,
|
|
),
|
|
},
|
|
},
|
|
validationOptions,
|
|
);
|
|
}
|