-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.ts
More file actions
58 lines (50 loc) · 1.96 KB
/
Copy pathvalidation.ts
File metadata and controls
58 lines (50 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Custom validation for this exercise,
* but for larger projects would evaluate third-party validation libraries like joy.
* Additionally, would look into options to generate openAPI validation off of an openAPI spec.
*/
import { APISymbol, Side, symbols, sides, NewOrderSingle } from "orders";
function isString(value: unknown): value is string {
return typeof value === "string";
}
function isNumber(value: unknown): value is number {
return typeof value === "number";
}
function isPositiveNumber(value: unknown): value is number {
return isNumber(value) && value > 0;
}
function isValidSymbol(value: unknown): value is APISymbol {
// case-insensitive validation
return isString(value) && symbols.has(value.toLocaleUpperCase());
}
function isValidSide(value: unknown): value is Side {
// case-insensitive validation
return isString(value) && sides.has(value.toLocaleLowerCase());
}
export function validateNewOrderSingle(order: unknown): NewOrderSingle | never {
// aggregating all validation errors instead of bailing on first
const errorMessages: Array<string> = [];
const { requestID, symbol, side, quantity, price } =
order as Partial<NewOrderSingle>;
if (!isString(requestID)) {
errorMessages.push(`requestID ${requestID} must be a string`);
}
if (!isValidSymbol(symbol)) {
errorMessages.push(`Unknown symbol ${symbol}`);
}
if (!isValidSide(side)) {
errorMessages.push(`Unknown side ${side}`);
}
if (!isPositiveNumber(quantity)) {
errorMessages.push(`quantity ${quantity} must be a positive number`);
}
if (!isPositiveNumber(price)) {
errorMessages.push(`price ${price} must be a positive number`);
}
if (errorMessages.length > 0) {
// this could be a custom ValidationError
throw new Error(`Validation error: ${errorMessages.join("\n")}`);
}
// returning a new object trimming additional properties (we can discuss this leniency)
return { requestID, symbol, side, quantity, price } as NewOrderSingle;
}