Skip to content

Commit

Permalink
updated OrderValidator for stop, limit and amount
Browse files Browse the repository at this point in the history
  • Loading branch information
egerj committed Apr 1, 2021
1 parent 0084bca commit e9e5dd2
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/order/OrderValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { UnprocessableEntityException } from '@nestjs/common';
import { PlaceOrderDto } from './dtos/PlaceOrder.dto';

const MAX_AMOUNT = 10000;
const MAX_DIGITS = 2;

export class OrderValidator {
public static validate(order: PlaceOrderDto): PlaceOrderDto {
const {
Expand All @@ -19,9 +22,10 @@ export class OrderValidator {
'Amount needs to be assigned and of type number',
);
}
if (amount < 0) {

if (amount < 0 || amount > MAX_AMOUNT) {
throw new UnprocessableEntityException(
'Amount must be greater than zero',
'Amount needs to be between 1 and 10000.',
);
}

Expand Down Expand Up @@ -61,6 +65,15 @@ export class OrderValidator {
);
}

if (limit && limit % 1 != 0) {
let limit_digits = limit.toString().split('.');
if (limit_digits[1].length > MAX_DIGITS) {
throw new UnprocessableEntityException(
'Limit cannot have more than 2 digits',
);
}
}

if (stop && typeof stop !== 'number') {
throw new UnprocessableEntityException('Stop needs to be of type number');
}
Expand All @@ -69,6 +82,15 @@ export class OrderValidator {
throw new UnprocessableEntityException('Stop must be greater than zero');
}

if (stop && stop % 1 != 0) {
let stop_digits = stop.toString().split('.');
if (stop_digits[1].length > MAX_DIGITS) {
throw new UnprocessableEntityException(
'Stop cannot have more than 2 digits',
);
}
}

return {
shareId: order.shareId,
amount: order.amount,
Expand Down

0 comments on commit e9e5dd2

Please sign in to comment.