@0xmonaco/types 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/api/index.d.ts.map +1 -1
  2. package/dist/applications/index.d.ts.map +1 -1
  3. package/dist/applications/responses.d.ts.map +1 -1
  4. package/dist/auth/index.d.ts.map +1 -1
  5. package/dist/auth/responses.d.ts.map +1 -1
  6. package/dist/contracts/balances.d.ts.map +1 -1
  7. package/dist/contracts/index.d.ts.map +1 -1
  8. package/dist/fees/index.d.ts.map +1 -1
  9. package/dist/fees/index.js.map +1 -1
  10. package/dist/fees/responses.d.ts.map +1 -1
  11. package/dist/fees/responses.js +0 -3
  12. package/dist/fees/responses.js.map +1 -1
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -0
  16. package/dist/index.js.map +1 -1
  17. package/dist/market/index.d.ts +4 -0
  18. package/dist/market/index.d.ts.map +1 -1
  19. package/dist/profile/index.d.ts +6 -7
  20. package/dist/profile/index.d.ts.map +1 -1
  21. package/dist/sdk/index.d.ts +5 -6
  22. package/dist/sdk/index.d.ts.map +1 -1
  23. package/dist/sdk/network.d.ts +5 -4
  24. package/dist/sdk/network.d.ts.map +1 -1
  25. package/dist/trading/index.d.ts.map +1 -1
  26. package/dist/trading/orders.d.ts.map +1 -1
  27. package/dist/validation/common.d.ts +181 -0
  28. package/dist/validation/common.d.ts.map +1 -0
  29. package/dist/validation/common.js +196 -0
  30. package/dist/validation/common.js.map +1 -0
  31. package/dist/validation/index.d.ts +20 -0
  32. package/dist/validation/index.d.ts.map +1 -0
  33. package/dist/validation/index.js +22 -0
  34. package/dist/validation/index.js.map +1 -0
  35. package/dist/validation/market.d.ts +169 -0
  36. package/dist/validation/market.d.ts.map +1 -0
  37. package/dist/validation/market.js +185 -0
  38. package/dist/validation/market.js.map +1 -0
  39. package/dist/validation/trading.d.ts +148 -0
  40. package/dist/validation/trading.d.ts.map +1 -0
  41. package/dist/validation/trading.js +139 -0
  42. package/dist/validation/trading.js.map +1 -0
  43. package/dist/validation/vault.d.ts +62 -0
  44. package/dist/validation/vault.d.ts.map +1 -0
  45. package/dist/validation/vault.js +75 -0
  46. package/dist/validation/vault.js.map +1 -0
  47. package/dist/vault/index.d.ts +4 -4
  48. package/dist/vault/index.d.ts.map +1 -1
  49. package/dist/vault/responses.d.ts.map +1 -1
  50. package/dist/websocket/events/orders-events.d.ts.map +1 -1
  51. package/package.json +2 -2
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Common Validation Utilities
3
+ *
4
+ * Shared utilities for runtime validation across all SDK modules.
5
+ * These utilities work with any Zod schema and provide consistent
6
+ * error handling throughout the SDK.
7
+ */
8
+ /**
9
+ * Validation error class for user-friendly error messages.
10
+ *
11
+ * Wraps Zod validation errors to provide better error formatting and
12
+ * structured error access for API consumers. This error is thrown when
13
+ * validation fails on any SDK parameters.
14
+ *
15
+ * **Features:**
16
+ * - Human-readable error message with field paths
17
+ * - Access to raw Zod issues for detailed error handling
18
+ * - Structured error object via `getErrors()` method
19
+ *
20
+ * **Example Usage:**
21
+ * ```typescript
22
+ * import { validate, PlaceLimitOrderSchema } from '@0xmonaco/types';
23
+ *
24
+ * try {
25
+ * const validated = validate(PlaceLimitOrderSchema, {
26
+ * tradingPairId: "invalid-uuid",
27
+ * side: "BUY",
28
+ * quantity: "-10", // Invalid: negative
29
+ * price: "100"
30
+ * });
31
+ * } catch (error) {
32
+ * if (error instanceof ValidationError) {
33
+ * // Human-readable message
34
+ * console.log(error.message);
35
+ * // "Validation failed:
36
+ * // - tradingPairId: Invalid uuid
37
+ * // - quantity: Number must be greater than 0"
38
+ *
39
+ * // Structured errors for form validation
40
+ * const errors = error.getErrors();
41
+ * console.log(errors);
42
+ * // {
43
+ * // "tradingPairId": "Invalid uuid",
44
+ * // "quantity": "Number must be greater than 0"
45
+ * // }
46
+ *
47
+ * // Raw Zod issues for advanced handling
48
+ * console.log(error.issues);
49
+ * // [{ path: ["tradingPairId"], message: "Invalid uuid", ... }, ...]
50
+ * }
51
+ * }
52
+ * ```
53
+ *
54
+ * @see {@link validate} - Helper function that throws ValidationError
55
+ */
56
+ export class ValidationError extends Error {
57
+ /**
58
+ * Creates a new ValidationError from a Zod validation error.
59
+ *
60
+ * Automatically formats all validation issues into a human-readable
61
+ * message and stores the raw issues for programmatic access.
62
+ *
63
+ * @param zodError - The Zod validation error to wrap
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * const result = schema.safeParse(data);
68
+ * if (!result.success) {
69
+ * throw new ValidationError(result.error);
70
+ * }
71
+ * ```
72
+ */
73
+ constructor(zodError) {
74
+ const messages = zodError.issues.map((issue) => {
75
+ const path = issue.path.join(".");
76
+ return `${path ? `${path}: ` : ""}${issue.message}`;
77
+ });
78
+ super(`Validation failed:\n - ${messages.join("\n - ")}`);
79
+ this.name = "ValidationError";
80
+ this.issues = zodError.issues;
81
+ }
82
+ /**
83
+ * Get validation errors as a structured object for easy field-level error display.
84
+ *
85
+ * Converts the array of Zod issues into a simple key-value object where
86
+ * keys are field paths (dot-notation) and values are error messages.
87
+ * Root-level errors (no path) are stored under the "root" key.
88
+ *
89
+ * **Use Cases:**
90
+ * - Form validation (map errors to input fields)
91
+ * - API error responses (structured JSON)
92
+ * - Error aggregation and reporting
93
+ *
94
+ * @returns Object mapping field paths to error messages
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * // Single field error
99
+ * error.getErrors();
100
+ * // { "price": "Number must be greater than 0" }
101
+ *
102
+ * // Multiple field errors
103
+ * error.getErrors();
104
+ * // {
105
+ * // "tradingPairId": "Invalid uuid",
106
+ * // "quantity": "Required",
107
+ * // "options.timeInForce": "Invalid enum value"
108
+ * // }
109
+ *
110
+ * // Root-level error (no specific field)
111
+ * error.getErrors();
112
+ * // { "root": "At least one field is required" }
113
+ *
114
+ * // Use in React form
115
+ * const errors = validationError.getErrors();
116
+ * <input error={errors.price} />
117
+ * ```
118
+ */
119
+ getErrors() {
120
+ const errors = {};
121
+ for (const issue of this.issues) {
122
+ const path = issue.path.join(".");
123
+ errors[path || "root"] = issue.message;
124
+ }
125
+ return errors;
126
+ }
127
+ }
128
+ /**
129
+ * Validates data against a Zod schema and throws user-friendly errors on failure.
130
+ *
131
+ * This is a convenience wrapper around Zod's `safeParse` that automatically
132
+ * throws a {@link ValidationError} with formatted error messages when validation fails.
133
+ *
134
+ * **When to Use:**
135
+ * - Validating user input before API calls
136
+ * - Runtime type checking for dynamic data
137
+ * - Ensuring data integrity before processing
138
+ *
139
+ * **Error Handling:**
140
+ * On validation failure, throws {@link ValidationError} which provides:
141
+ * - Human-readable error message
142
+ * - Structured errors via `getErrors()`
143
+ * - Raw Zod issues for advanced handling
144
+ *
145
+ * @template T - The expected type after validation
146
+ * @param schema - Zod schema to validate against
147
+ * @param data - Data to validate (unknown type for safety)
148
+ * @returns Validated and typed data
149
+ * @throws {@link ValidationError} When validation fails
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * import { validate, PlaceLimitOrderSchema } from '@0xmonaco/types';
154
+ *
155
+ * // Basic validation
156
+ * try {
157
+ * const validatedOrder = validate(PlaceLimitOrderSchema, userInput);
158
+ * // validatedOrder is now typed and guaranteed valid
159
+ * await sdk.trading.placeLimitOrder(...validatedOrder);
160
+ * } catch (error) {
161
+ * if (error instanceof ValidationError) {
162
+ * // Show user-friendly errors
163
+ * console.error(error.message);
164
+ * const fieldErrors = error.getErrors();
165
+ * // Display errors next to form fields
166
+ * }
167
+ * }
168
+ *
169
+ * // With custom error handling
170
+ * function safeValidate<T>(schema: z.ZodSchema<T>, data: unknown) {
171
+ * try {
172
+ * return { success: true, data: validate(schema, data) };
173
+ * } catch (error) {
174
+ * if (error instanceof ValidationError) {
175
+ * return { success: false, errors: error.getErrors() };
176
+ * }
177
+ * throw error; // Re-throw unexpected errors
178
+ * }
179
+ * }
180
+ *
181
+ * const result = safeValidate(PlaceLimitOrderSchema, input);
182
+ * if (result.success) {
183
+ * // Use result.data
184
+ * } else {
185
+ * // Handle result.errors
186
+ * }
187
+ * ```
188
+ */
189
+ export function validate(schema, data) {
190
+ const result = schema.safeParse(data);
191
+ if (!result.success) {
192
+ throw new ValidationError(result.error);
193
+ }
194
+ return result.data;
195
+ }
196
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/validation/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAOxC;;;;;;;;;;;;;;;OAeG;IACH,YAAY,QAAoB;QAC9B,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAClC,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;QACtD,CAAC,CAAC,CAAC;QAEH,KAAK,CAAC,2BAA2B,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC5D,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;IAChC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,SAAS;QACP,MAAM,MAAM,GAA2B,EAAE,CAAC;QAC1C,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAClC,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC;QACzC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,MAAM,UAAU,QAAQ,CAAI,MAAsB,EAAE,IAAa;IAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAEtC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,IAAI,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Validation Schemas
3
+ *
4
+ * Runtime validation using Zod for all SDK inputs.
5
+ * Catches errors before making API calls.
6
+ *
7
+ * **Shared Utilities:**
8
+ * - {@link ValidationError} - User-friendly validation error class
9
+ * - {@link validate} - Helper function for schema validation
10
+ *
11
+ * **Schema Modules:**
12
+ * - Trading schemas (orders, trading pairs)
13
+ * - Vault schemas (deposits, withdrawals)
14
+ * - Market schemas (trading pairs, candlesticks)
15
+ */
16
+ export * from "./common.js";
17
+ export * from "./trading.js";
18
+ export * from "./vault.js";
19
+ export * from "./market.js";
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,cAAc,aAAa,CAAC;AAG5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Validation Schemas
3
+ *
4
+ * Runtime validation using Zod for all SDK inputs.
5
+ * Catches errors before making API calls.
6
+ *
7
+ * **Shared Utilities:**
8
+ * - {@link ValidationError} - User-friendly validation error class
9
+ * - {@link validate} - Helper function for schema validation
10
+ *
11
+ * **Schema Modules:**
12
+ * - Trading schemas (orders, trading pairs)
13
+ * - Vault schemas (deposits, withdrawals)
14
+ * - Market schemas (trading pairs, candlesticks)
15
+ */
16
+ // Export validation utilities
17
+ export * from "./common.js";
18
+ // Export module-specific schemas
19
+ export * from "./trading.js";
20
+ export * from "./vault.js";
21
+ export * from "./market.js";
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,8BAA8B;AAC9B,cAAc,aAAa,CAAC;AAE5B,iCAAiC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Market API Validation Schemas
3
+ */
4
+ import { z } from "zod";
5
+ /**
6
+ * Candlestick interval validation
7
+ */
8
+ export declare const IntervalSchema: z.ZodEnum<{
9
+ "1m": "1m";
10
+ "5m": "5m";
11
+ "15m": "15m";
12
+ "1h": "1h";
13
+ "4h": "4h";
14
+ "1d": "1d";
15
+ "30m": "30m";
16
+ "1w": "1w";
17
+ "1M": "1M";
18
+ }>;
19
+ /**
20
+ * Timestamp validation (milliseconds since epoch)
21
+ *
22
+ * Validates that the timestamp is:
23
+ * - A positive integer
24
+ * - At least Jan 1, 2000 (catches seconds vs milliseconds errors)
25
+ * - Within JavaScript's safe integer range
26
+ * - Not unreasonably far in the future (max 1 year ahead)
27
+ *
28
+ * **Why these bounds matter:**
29
+ * - Prevents typos (e.g., using seconds instead of milliseconds: 1609459200 vs 1609459200000)
30
+ * - Catches date calculation errors (e.g., Date.now() * 1000 instead of Date.now())
31
+ * - Ensures timestamps won't cause precision issues in JavaScript
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // Valid timestamps (milliseconds)
36
+ * validate(TimestampSchema, 1609459200000); // Jan 1, 2021 ✅
37
+ * validate(TimestampSchema, Date.now()); // Current time ✅
38
+ * validate(TimestampSchema, Date.now() + 86400000); // Tomorrow ✅
39
+ *
40
+ * // Invalid timestamps
41
+ * validate(TimestampSchema, 1609459200); // Seconds instead of ms (Jan 19, 1970) ❌
42
+ * validate(TimestampSchema, Date.now() * 1000); // Way too large ❌
43
+ * validate(TimestampSchema, 9999999999999999); // Far future (year 2286) ❌
44
+ * validate(TimestampSchema, 100000); // Too old (Jan 1, 1970) ❌
45
+ * ```
46
+ */
47
+ export declare const TimestampSchema: z.ZodNumber;
48
+ /**
49
+ * Get Candlesticks validation schema (by trading pair ID)
50
+ *
51
+ * Validates parameters for fetching candlestick (OHLCV) data using a trading pair UUID.
52
+ * Ensures that the time range is valid by checking that startTime < endTime.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * // Valid request
57
+ * validate(GetCandlesticksSchema, {
58
+ * tradingPairId: "123e4567-e89b-12d3-a456-426614174000",
59
+ * interval: "1h",
60
+ * startTime: 1609459200000, // Jan 1, 2021
61
+ * endTime: 1640995200000 // Jan 1, 2022
62
+ * });
63
+ *
64
+ * // Invalid: backwards time range
65
+ * validate(GetCandlesticksSchema, {
66
+ * tradingPairId: "123e4567-e89b-12d3-a456-426614174000",
67
+ * interval: "1h",
68
+ * startTime: 1640995200000, // Jan 1, 2022
69
+ * endTime: 1609459200000 // Jan 1, 2021
70
+ * });
71
+ * // ValidationError: startTime must be less than endTime
72
+ * ```
73
+ */
74
+ export declare const GetCandlesticksSchema: z.ZodObject<{
75
+ tradingPairId: z.ZodString;
76
+ interval: z.ZodEnum<{
77
+ "1m": "1m";
78
+ "5m": "5m";
79
+ "15m": "15m";
80
+ "1h": "1h";
81
+ "4h": "4h";
82
+ "1d": "1d";
83
+ "30m": "30m";
84
+ "1w": "1w";
85
+ "1M": "1M";
86
+ }>;
87
+ startTime: z.ZodNumber;
88
+ endTime: z.ZodNumber;
89
+ }, z.core.$strip>;
90
+ /**
91
+ * Trading pair symbol validation
92
+ *
93
+ * Validates trading pair symbols like "BTC/USDT", "ETH/USD", etc.
94
+ * Symbol format: BASE/QUOTE where:
95
+ * - BASE and QUOTE are 1-10 alphanumeric characters
96
+ * - Separated by a forward slash
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * // Valid symbols
101
+ * validate(TradingPairSymbolSchema, "BTC/USDT"); ✅
102
+ * validate(TradingPairSymbolSchema, "ETH/USD"); ✅
103
+ * validate(TradingPairSymbolSchema, "MTK/USDCo"); ✅
104
+ *
105
+ * // Invalid symbols
106
+ * validate(TradingPairSymbolSchema, "BTC"); ❌ Missing quote token
107
+ * validate(TradingPairSymbolSchema, "BTC-USDT"); ❌ Wrong separator
108
+ * validate(TradingPairSymbolSchema, "/USDT"); ❌ Missing base token
109
+ * ```
110
+ */
111
+ export declare const TradingPairSymbolSchema: z.ZodString;
112
+ /**
113
+ * Get Candlesticks By Symbol validation schema
114
+ *
115
+ * Validates parameters for fetching candlestick (OHLCV) data using a trading pair symbol.
116
+ * This is the schema used by the actual API endpoint which accepts symbols like "BTC/USDT".
117
+ * Ensures that the time range is valid by checking that startTime < endTime.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * // Valid request
122
+ * validate(GetCandlesticksBySymbolSchema, {
123
+ * symbol: "BTC/USDT",
124
+ * interval: "1h",
125
+ * startTime: 1609459200000, // Jan 1, 2021
126
+ * endTime: 1640995200000 // Jan 1, 2022
127
+ * });
128
+ *
129
+ * // Invalid: backwards time range
130
+ * validate(GetCandlesticksBySymbolSchema, {
131
+ * symbol: "ETH/USD",
132
+ * interval: "1h",
133
+ * startTime: 1640995200000, // Jan 1, 2022
134
+ * endTime: 1609459200000 // Jan 1, 2021
135
+ * });
136
+ * // ValidationError: startTime must be less than endTime
137
+ * ```
138
+ */
139
+ export declare const GetCandlesticksBySymbolSchema: z.ZodObject<{
140
+ symbol: z.ZodString;
141
+ interval: z.ZodEnum<{
142
+ "1m": "1m";
143
+ "5m": "5m";
144
+ "15m": "15m";
145
+ "1h": "1h";
146
+ "4h": "4h";
147
+ "1d": "1d";
148
+ "30m": "30m";
149
+ "1w": "1w";
150
+ "1M": "1M";
151
+ }>;
152
+ startTime: z.ZodNumber;
153
+ endTime: z.ZodNumber;
154
+ }, z.core.$strip>;
155
+ /**
156
+ * Get Trading Pair validation schema
157
+ */
158
+ export declare const GetTradingPairSchema: z.ZodObject<{
159
+ tradingPairId: z.ZodString;
160
+ }, z.core.$strip>;
161
+ /**
162
+ * Search Trading Pairs validation schema
163
+ */
164
+ export declare const SearchTradingPairsSchema: z.ZodObject<{
165
+ query: z.ZodOptional<z.ZodString>;
166
+ limit: z.ZodOptional<z.ZodNumber>;
167
+ offset: z.ZodOptional<z.ZodNumber>;
168
+ }, z.core.$strip>;
169
+ //# sourceMappingURL=market.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../../src/validation/market.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;GAEG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;EAEzB,CAAC;AAkBH;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,eAAe,aAcxB,CAAC;AAEL;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;iBAU9B,CAAC;AAEL;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,uBAAuB,aAKhC,CAAC;AAEL;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,6BAA6B;;;;;;;;;;;;;;;iBAUtC,CAAC;AAEL;;GAEG;AACH,eAAO,MAAM,oBAAoB;;iBAE/B,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,wBAAwB;;;;iBAInC,CAAC"}
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Market API Validation Schemas
3
+ */
4
+ import { z } from "zod";
5
+ import { UUIDSchema } from "./trading";
6
+ /**
7
+ * Candlestick interval validation
8
+ */
9
+ export const IntervalSchema = z.enum(["1m", "5m", "15m", "30m", "1h", "4h", "1d", "1w", "1M"], {
10
+ message: 'Interval must be one of: "1m", "5m", "15m", "30m", "1h", "4h", "1d", "1w", "1M"',
11
+ });
12
+ /**
13
+ * Maximum allowed future offset for timestamps (1 year in milliseconds)
14
+ *
15
+ * Allows queries for future dates (e.g., for scheduling or filtering) but
16
+ * prevents unreasonably far future dates that are likely input errors.
17
+ */
18
+ const MAX_TIMESTAMP_FUTURE_OFFSET_MS = 365 * 24 * 60 * 60 * 1000; // 1 year
19
+ /**
20
+ * Minimum allowed timestamp (Jan 1, 2000 in milliseconds)
21
+ *
22
+ * This catches the common error of passing timestamps in seconds instead of milliseconds.
23
+ * Any timestamp before year 2000 is likely an error (either seconds or invalid date).
24
+ */
25
+ const MIN_TIMESTAMP_MS = 946684800000; // Jan 1, 2000 00:00:00 UTC
26
+ /**
27
+ * Timestamp validation (milliseconds since epoch)
28
+ *
29
+ * Validates that the timestamp is:
30
+ * - A positive integer
31
+ * - At least Jan 1, 2000 (catches seconds vs milliseconds errors)
32
+ * - Within JavaScript's safe integer range
33
+ * - Not unreasonably far in the future (max 1 year ahead)
34
+ *
35
+ * **Why these bounds matter:**
36
+ * - Prevents typos (e.g., using seconds instead of milliseconds: 1609459200 vs 1609459200000)
37
+ * - Catches date calculation errors (e.g., Date.now() * 1000 instead of Date.now())
38
+ * - Ensures timestamps won't cause precision issues in JavaScript
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Valid timestamps (milliseconds)
43
+ * validate(TimestampSchema, 1609459200000); // Jan 1, 2021 ✅
44
+ * validate(TimestampSchema, Date.now()); // Current time ✅
45
+ * validate(TimestampSchema, Date.now() + 86400000); // Tomorrow ✅
46
+ *
47
+ * // Invalid timestamps
48
+ * validate(TimestampSchema, 1609459200); // Seconds instead of ms (Jan 19, 1970) ❌
49
+ * validate(TimestampSchema, Date.now() * 1000); // Way too large ❌
50
+ * validate(TimestampSchema, 9999999999999999); // Far future (year 2286) ❌
51
+ * validate(TimestampSchema, 100000); // Too old (Jan 1, 1970) ❌
52
+ * ```
53
+ */
54
+ export const TimestampSchema = z
55
+ .number()
56
+ .int()
57
+ .positive({
58
+ message: "Timestamp must be a positive integer (milliseconds since epoch)",
59
+ })
60
+ .min(MIN_TIMESTAMP_MS, {
61
+ message: "Timestamp must be at least Jan 1, 2000 (946684800000 ms). Did you pass seconds instead of milliseconds?",
62
+ })
63
+ .max(Number.MAX_SAFE_INTEGER, {
64
+ message: "Timestamp must not exceed JavaScript's maximum safe integer",
65
+ })
66
+ .refine((value) => value <= Date.now() + MAX_TIMESTAMP_FUTURE_OFFSET_MS, {
67
+ message: "Timestamp must not be unreasonably far in the future (max 1 year ahead)",
68
+ });
69
+ /**
70
+ * Get Candlesticks validation schema (by trading pair ID)
71
+ *
72
+ * Validates parameters for fetching candlestick (OHLCV) data using a trading pair UUID.
73
+ * Ensures that the time range is valid by checking that startTime < endTime.
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * // Valid request
78
+ * validate(GetCandlesticksSchema, {
79
+ * tradingPairId: "123e4567-e89b-12d3-a456-426614174000",
80
+ * interval: "1h",
81
+ * startTime: 1609459200000, // Jan 1, 2021
82
+ * endTime: 1640995200000 // Jan 1, 2022
83
+ * });
84
+ *
85
+ * // Invalid: backwards time range
86
+ * validate(GetCandlesticksSchema, {
87
+ * tradingPairId: "123e4567-e89b-12d3-a456-426614174000",
88
+ * interval: "1h",
89
+ * startTime: 1640995200000, // Jan 1, 2022
90
+ * endTime: 1609459200000 // Jan 1, 2021
91
+ * });
92
+ * // ValidationError: startTime must be less than endTime
93
+ * ```
94
+ */
95
+ export const GetCandlesticksSchema = z
96
+ .object({
97
+ tradingPairId: UUIDSchema,
98
+ interval: IntervalSchema,
99
+ startTime: TimestampSchema,
100
+ endTime: TimestampSchema,
101
+ })
102
+ .refine((data) => data.startTime < data.endTime, {
103
+ message: "startTime must be less than endTime",
104
+ path: ["endTime"],
105
+ });
106
+ /**
107
+ * Trading pair symbol validation
108
+ *
109
+ * Validates trading pair symbols like "BTC/USDT", "ETH/USD", etc.
110
+ * Symbol format: BASE/QUOTE where:
111
+ * - BASE and QUOTE are 1-10 alphanumeric characters
112
+ * - Separated by a forward slash
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * // Valid symbols
117
+ * validate(TradingPairSymbolSchema, "BTC/USDT"); ✅
118
+ * validate(TradingPairSymbolSchema, "ETH/USD"); ✅
119
+ * validate(TradingPairSymbolSchema, "MTK/USDCo"); ✅
120
+ *
121
+ * // Invalid symbols
122
+ * validate(TradingPairSymbolSchema, "BTC"); ❌ Missing quote token
123
+ * validate(TradingPairSymbolSchema, "BTC-USDT"); ❌ Wrong separator
124
+ * validate(TradingPairSymbolSchema, "/USDT"); ❌ Missing base token
125
+ * ```
126
+ */
127
+ export const TradingPairSymbolSchema = z
128
+ .string()
129
+ .min(1, "Trading pair symbol cannot be empty")
130
+ .regex(/^[A-Za-z0-9]{1,10}\/[A-Za-z0-9]{1,10}$/, {
131
+ message: 'Trading pair symbol must be in format "BASE/QUOTE" (e.g., "BTC/USDT", "ETH/USD")',
132
+ });
133
+ /**
134
+ * Get Candlesticks By Symbol validation schema
135
+ *
136
+ * Validates parameters for fetching candlestick (OHLCV) data using a trading pair symbol.
137
+ * This is the schema used by the actual API endpoint which accepts symbols like "BTC/USDT".
138
+ * Ensures that the time range is valid by checking that startTime < endTime.
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * // Valid request
143
+ * validate(GetCandlesticksBySymbolSchema, {
144
+ * symbol: "BTC/USDT",
145
+ * interval: "1h",
146
+ * startTime: 1609459200000, // Jan 1, 2021
147
+ * endTime: 1640995200000 // Jan 1, 2022
148
+ * });
149
+ *
150
+ * // Invalid: backwards time range
151
+ * validate(GetCandlesticksBySymbolSchema, {
152
+ * symbol: "ETH/USD",
153
+ * interval: "1h",
154
+ * startTime: 1640995200000, // Jan 1, 2022
155
+ * endTime: 1609459200000 // Jan 1, 2021
156
+ * });
157
+ * // ValidationError: startTime must be less than endTime
158
+ * ```
159
+ */
160
+ export const GetCandlesticksBySymbolSchema = z
161
+ .object({
162
+ symbol: TradingPairSymbolSchema,
163
+ interval: IntervalSchema,
164
+ startTime: TimestampSchema,
165
+ endTime: TimestampSchema,
166
+ })
167
+ .refine((data) => data.startTime < data.endTime, {
168
+ message: "startTime must be less than endTime",
169
+ path: ["endTime"],
170
+ });
171
+ /**
172
+ * Get Trading Pair validation schema
173
+ */
174
+ export const GetTradingPairSchema = z.object({
175
+ tradingPairId: UUIDSchema,
176
+ });
177
+ /**
178
+ * Search Trading Pairs validation schema
179
+ */
180
+ export const SearchTradingPairsSchema = z.object({
181
+ query: z.string().min(1, "Search query cannot be empty").optional(),
182
+ limit: z.number().int().min(1).max(100).optional(),
183
+ offset: z.number().int().min(0).optional(),
184
+ });
185
+ //# sourceMappingURL=market.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"market.js","sourceRoot":"","sources":["../../src/validation/market.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE;IAC7F,OAAO,EAAE,iFAAiF;CAC3F,CAAC,CAAC;AAEH;;;;;GAKG;AACH,MAAM,8BAA8B,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;AAE3E;;;;;GAKG;AACH,MAAM,gBAAgB,GAAG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