@0xmonaco/types 0.4.1 → 0.5.0
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.
- package/README.md +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/applications/index.d.ts +3 -3
- package/dist/applications/index.d.ts.map +1 -1
- package/dist/applications/responses.d.ts.map +1 -1
- package/dist/auth/index.d.ts +3 -3
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/responses.d.ts.map +1 -1
- package/dist/contracts/balances.d.ts.map +1 -1
- package/dist/contracts/index.d.ts +1 -1
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/fees/index.d.ts +4 -4
- package/dist/fees/index.d.ts.map +1 -1
- package/dist/fees/index.js +1 -1
- package/dist/fees/index.js.map +1 -1
- package/dist/fees/responses.d.ts.map +1 -1
- package/dist/fees/responses.js +0 -3
- package/dist/fees/responses.js.map +1 -1
- package/dist/index.d.ts +12 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -11
- package/dist/index.js.map +1 -1
- package/dist/market/index.d.ts +5 -1
- package/dist/market/index.d.ts.map +1 -1
- package/dist/profile/index.d.ts +8 -9
- package/dist/profile/index.d.ts.map +1 -1
- package/dist/sdk/index.d.ts +53 -31
- package/dist/sdk/index.d.ts.map +1 -1
- package/dist/sdk/network.d.ts.map +1 -1
- package/dist/trading/index.d.ts +5 -5
- package/dist/trading/index.d.ts.map +1 -1
- package/dist/trading/orders.d.ts.map +1 -1
- package/dist/trading/responses.d.ts +1 -1
- package/dist/trading/responses.d.ts.map +1 -1
- package/dist/validation/common.d.ts +181 -0
- package/dist/validation/common.d.ts.map +1 -0
- package/dist/validation/common.js +196 -0
- package/dist/validation/common.js.map +1 -0
- package/dist/validation/index.d.ts +20 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +22 -0
- package/dist/validation/index.js.map +1 -0
- package/dist/validation/market.d.ts +169 -0
- package/dist/validation/market.d.ts.map +1 -0
- package/dist/validation/market.js +185 -0
- package/dist/validation/market.js.map +1 -0
- package/dist/validation/trading.d.ts +148 -0
- package/dist/validation/trading.d.ts.map +1 -0
- package/dist/validation/trading.js +139 -0
- package/dist/validation/trading.js.map +1 -0
- package/dist/validation/vault.d.ts +62 -0
- package/dist/validation/vault.d.ts.map +1 -0
- package/dist/validation/vault.js +75 -0
- package/dist/validation/vault.js.map +1 -0
- package/dist/vault/index.d.ts +7 -7
- package/dist/vault/index.d.ts.map +1 -1
- package/dist/vault/responses.d.ts.map +1 -1
- package/dist/websocket/base.d.ts +5 -73
- package/dist/websocket/base.d.ts.map +1 -1
- package/dist/websocket/base.js +4 -11
- package/dist/websocket/base.js.map +1 -1
- package/dist/websocket/clients/orderbook-client.d.ts +1 -26
- package/dist/websocket/clients/orderbook-client.d.ts.map +1 -1
- package/dist/websocket/clients/orderbook-client.js +1 -1
- package/dist/websocket/events/index.d.ts +4 -3
- package/dist/websocket/events/index.d.ts.map +1 -1
- package/dist/websocket/events/index.js +4 -3
- package/dist/websocket/events/index.js.map +1 -1
- package/dist/websocket/events/ohlcv-events.d.ts +2 -2
- package/dist/websocket/events/ohlcv-events.d.ts.map +1 -1
- package/dist/websocket/events/orderbook-events.d.ts +4 -4
- package/dist/websocket/events/orderbook-events.d.ts.map +1 -1
- package/dist/websocket/events/orders-events.d.ts +2 -2
- package/dist/websocket/events/orders-events.d.ts.map +1 -1
- package/dist/websocket/events/trade-events.d.ts +35 -0
- package/dist/websocket/events/trade-events.d.ts.map +1 -0
- package/dist/websocket/events/trade-events.js +7 -0
- package/dist/websocket/events/trade-events.js.map +1 -0
- package/dist/websocket/index.d.ts +3 -13
- package/dist/websocket/index.d.ts.map +1 -1
- package/dist/websocket/index.js +3 -14
- package/dist/websocket/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/websocket/clients/ohlcv-client.d.ts +0 -28
- package/dist/websocket/clients/ohlcv-client.d.ts.map +0 -1
- package/dist/websocket/clients/ohlcv-client.js +0 -5
- package/dist/websocket/clients/ohlcv-client.js.map +0 -1
- package/dist/websocket/clients/orders-client.d.ts +0 -25
- package/dist/websocket/clients/orders-client.d.ts.map +0 -1
- package/dist/websocket/clients/orders-client.js +0 -5
- package/dist/websocket/clients/orders-client.js.map +0 -1
|
@@ -0,0 +1,181 @@
|
|
|
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
|
+
import { z } from "zod";
|
|
9
|
+
/**
|
|
10
|
+
* Validation error class for user-friendly error messages.
|
|
11
|
+
*
|
|
12
|
+
* Wraps Zod validation errors to provide better error formatting and
|
|
13
|
+
* structured error access for API consumers. This error is thrown when
|
|
14
|
+
* validation fails on any SDK parameters.
|
|
15
|
+
*
|
|
16
|
+
* **Features:**
|
|
17
|
+
* - Human-readable error message with field paths
|
|
18
|
+
* - Access to raw Zod issues for detailed error handling
|
|
19
|
+
* - Structured error object via `getErrors()` method
|
|
20
|
+
*
|
|
21
|
+
* **Example Usage:**
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { validate, PlaceLimitOrderSchema } from '@0xmonaco/types';
|
|
24
|
+
*
|
|
25
|
+
* try {
|
|
26
|
+
* const validated = validate(PlaceLimitOrderSchema, {
|
|
27
|
+
* tradingPairId: "invalid-uuid",
|
|
28
|
+
* side: "BUY",
|
|
29
|
+
* quantity: "-10", // Invalid: negative
|
|
30
|
+
* price: "100"
|
|
31
|
+
* });
|
|
32
|
+
* } catch (error) {
|
|
33
|
+
* if (error instanceof ValidationError) {
|
|
34
|
+
* // Human-readable message
|
|
35
|
+
* console.log(error.message);
|
|
36
|
+
* // "Validation failed:
|
|
37
|
+
* // - tradingPairId: Invalid uuid
|
|
38
|
+
* // - quantity: Number must be greater than 0"
|
|
39
|
+
*
|
|
40
|
+
* // Structured errors for form validation
|
|
41
|
+
* const errors = error.getErrors();
|
|
42
|
+
* console.log(errors);
|
|
43
|
+
* // {
|
|
44
|
+
* // "tradingPairId": "Invalid uuid",
|
|
45
|
+
* // "quantity": "Number must be greater than 0"
|
|
46
|
+
* // }
|
|
47
|
+
*
|
|
48
|
+
* // Raw Zod issues for advanced handling
|
|
49
|
+
* console.log(error.issues);
|
|
50
|
+
* // [{ path: ["tradingPairId"], message: "Invalid uuid", ... }, ...]
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @see {@link validate} - Helper function that throws ValidationError
|
|
56
|
+
*/
|
|
57
|
+
export declare class ValidationError extends Error {
|
|
58
|
+
/**
|
|
59
|
+
* Array of Zod validation issues containing detailed error information.
|
|
60
|
+
* Each issue includes the field path, error code, and message.
|
|
61
|
+
*/
|
|
62
|
+
readonly issues: z.ZodIssue[];
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new ValidationError from a Zod validation error.
|
|
65
|
+
*
|
|
66
|
+
* Automatically formats all validation issues into a human-readable
|
|
67
|
+
* message and stores the raw issues for programmatic access.
|
|
68
|
+
*
|
|
69
|
+
* @param zodError - The Zod validation error to wrap
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const result = schema.safeParse(data);
|
|
74
|
+
* if (!result.success) {
|
|
75
|
+
* throw new ValidationError(result.error);
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
constructor(zodError: z.ZodError);
|
|
80
|
+
/**
|
|
81
|
+
* Get validation errors as a structured object for easy field-level error display.
|
|
82
|
+
*
|
|
83
|
+
* Converts the array of Zod issues into a simple key-value object where
|
|
84
|
+
* keys are field paths (dot-notation) and values are error messages.
|
|
85
|
+
* Root-level errors (no path) are stored under the "root" key.
|
|
86
|
+
*
|
|
87
|
+
* **Use Cases:**
|
|
88
|
+
* - Form validation (map errors to input fields)
|
|
89
|
+
* - API error responses (structured JSON)
|
|
90
|
+
* - Error aggregation and reporting
|
|
91
|
+
*
|
|
92
|
+
* @returns Object mapping field paths to error messages
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* // Single field error
|
|
97
|
+
* error.getErrors();
|
|
98
|
+
* // { "price": "Number must be greater than 0" }
|
|
99
|
+
*
|
|
100
|
+
* // Multiple field errors
|
|
101
|
+
* error.getErrors();
|
|
102
|
+
* // {
|
|
103
|
+
* // "tradingPairId": "Invalid uuid",
|
|
104
|
+
* // "quantity": "Required",
|
|
105
|
+
* // "options.timeInForce": "Invalid enum value"
|
|
106
|
+
* // }
|
|
107
|
+
*
|
|
108
|
+
* // Root-level error (no specific field)
|
|
109
|
+
* error.getErrors();
|
|
110
|
+
* // { "root": "At least one field is required" }
|
|
111
|
+
*
|
|
112
|
+
* // Use in React form
|
|
113
|
+
* const errors = validationError.getErrors();
|
|
114
|
+
* <input error={errors.price} />
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
getErrors(): Record<string, string>;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Validates data against a Zod schema and throws user-friendly errors on failure.
|
|
121
|
+
*
|
|
122
|
+
* This is a convenience wrapper around Zod's `safeParse` that automatically
|
|
123
|
+
* throws a {@link ValidationError} with formatted error messages when validation fails.
|
|
124
|
+
*
|
|
125
|
+
* **When to Use:**
|
|
126
|
+
* - Validating user input before API calls
|
|
127
|
+
* - Runtime type checking for dynamic data
|
|
128
|
+
* - Ensuring data integrity before processing
|
|
129
|
+
*
|
|
130
|
+
* **Error Handling:**
|
|
131
|
+
* On validation failure, throws {@link ValidationError} which provides:
|
|
132
|
+
* - Human-readable error message
|
|
133
|
+
* - Structured errors via `getErrors()`
|
|
134
|
+
* - Raw Zod issues for advanced handling
|
|
135
|
+
*
|
|
136
|
+
* @template T - The expected type after validation
|
|
137
|
+
* @param schema - Zod schema to validate against
|
|
138
|
+
* @param data - Data to validate (unknown type for safety)
|
|
139
|
+
* @returns Validated and typed data
|
|
140
|
+
* @throws {@link ValidationError} When validation fails
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* import { validate, PlaceLimitOrderSchema } from '@0xmonaco/types';
|
|
145
|
+
*
|
|
146
|
+
* // Basic validation
|
|
147
|
+
* try {
|
|
148
|
+
* const validatedOrder = validate(PlaceLimitOrderSchema, userInput);
|
|
149
|
+
* // validatedOrder is now typed and guaranteed valid
|
|
150
|
+
* await sdk.trading.placeLimitOrder(...validatedOrder);
|
|
151
|
+
* } catch (error) {
|
|
152
|
+
* if (error instanceof ValidationError) {
|
|
153
|
+
* // Show user-friendly errors
|
|
154
|
+
* console.error(error.message);
|
|
155
|
+
* const fieldErrors = error.getErrors();
|
|
156
|
+
* // Display errors next to form fields
|
|
157
|
+
* }
|
|
158
|
+
* }
|
|
159
|
+
*
|
|
160
|
+
* // With custom error handling
|
|
161
|
+
* function safeValidate<T>(schema: z.ZodSchema<T>, data: unknown) {
|
|
162
|
+
* try {
|
|
163
|
+
* return { success: true, data: validate(schema, data) };
|
|
164
|
+
* } catch (error) {
|
|
165
|
+
* if (error instanceof ValidationError) {
|
|
166
|
+
* return { success: false, errors: error.getErrors() };
|
|
167
|
+
* }
|
|
168
|
+
* throw error; // Re-throw unexpected errors
|
|
169
|
+
* }
|
|
170
|
+
* }
|
|
171
|
+
*
|
|
172
|
+
* const result = safeValidate(PlaceLimitOrderSchema, input);
|
|
173
|
+
* if (result.success) {
|
|
174
|
+
* // Use result.data
|
|
175
|
+
* } else {
|
|
176
|
+
* // Handle result.errors
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare function validate<T>(schema: z.ZodSchema<T>, data: unknown): T;
|
|
181
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/validation/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC;;;OAGG;IACH,SAAgB,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;IAErC;;;;;;;;;;;;;;;OAeG;gBACS,QAAQ,EAAE,CAAC,CAAC,QAAQ;IAWhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;CAQpC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,GAAG,CAAC,CAQpE"}
|
|
@@ -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"}
|