Browse Source

Allow negative quantity in DTO for SPLIT type

pull/3211/head
Nicolas Fedor 1 year ago
parent
commit
c014712e30
  1. 27
      apps/api/src/app/order/create-order.dto.ts

27
apps/api/src/app/order/create-order.dto.ts

@ -15,10 +15,33 @@ import {
IsNumber,
IsOptional,
IsString,
Min
Min,
ValidationArguments,
ValidationOptions,
registerDecorator
} from 'class-validator';
import { isString } from 'lodash';
function IsQuantityValid(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
registerDecorator({
name: 'isQuantityValid',
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
const order = args.object as CreateOrderDto;
return order.type !== 'SPLIT' ? value >= 0 : true;
},
defaultMessage(args: ValidationArguments) {
return `Quantity must not be less than 0 unless the type is ${Type.SPLIT}`;
}
}
});
};
}
export class CreateOrderDto {
@IsOptional()
@IsString()
@ -54,7 +77,7 @@ export class CreateOrderDto {
fee: number;
@IsNumber()
@Min(0)
@IsQuantityValid()
quantity: number;
@IsString()

Loading…
Cancel
Save