/** * Common Validation Utilities * * Shared utilities for runtime validation across all SDK modules. * These utilities work with any Zod schema and provide consistent * error handling throughout the SDK. */ import type { z } from "zod"; /** * Validation error class for user-friendly error messages. * * Wraps Zod validation errors to provide better error formatting and * structured error access for API consumers. This error is thrown when * validation fails on any SDK parameters. * * **Features:** * - Human-readable error message with field paths * - Access to raw Zod issues for detailed error handling * - Structured error object via `getErrors()` method * * **Example Usage:** * ```typescript * import { validate, PlaceLimitOrderSchema } from '@0xmonaco/types'; * * try { * const validated = validate(PlaceLimitOrderSchema, { * tradingPairId: "invalid-uuid", * side: "BUY", * quantity: "-10", // Invalid: negative * price: "100" * }); * } catch (error) { * if (error instanceof ValidationError) { * // Human-readable message * console.log(error.message); * // "Validation failed: * // - tradingPairId: Invalid uuid * // - quantity: Number must be greater than 0" * * // Structured errors for form validation * const errors = error.getErrors(); * console.log(errors); * // { * // "tradingPairId": "Invalid uuid", * // "quantity": "Number must be greater than 0" * // } * * // Raw Zod issues for advanced handling * console.log(error.issues); * } * } * ``` * * @see {@link validate} - Helper function that throws ValidationError */ export declare class ValidationError extends Error { /** * Array of Zod validation issues containing detailed error information. * Each issue includes the field path, error code, and message. */ readonly issues: z.core.$ZodIssue[]; /** * Creates a new ValidationError from a Zod validation error. * * Automatically formats all validation issues into a human-readable * message and stores the raw issues for programmatic access. * * @param zodError - The Zod validation error to wrap * * @example * ```typescript * const result = schema.safeParse(data); * if (!result.success) { * throw new ValidationError(result.error); * } * ``` */ constructor(zodError: z.ZodError); /** * Get validation errors as a structured object for easy field-level error display. * * Converts the array of Zod issues into a simple key-value object where * keys are field paths (dot-notation) and values are error messages. * Root-level errors (no path) are stored under the "root" key. * * **Use Cases:** * - Form validation (map errors to input fields) * - API error responses (structured JSON) * - Error aggregation and reporting * * @returns Object mapping field paths to error messages * * @example * ```typescript * // Single field errors * error.getErrors(); * // { "price": "Number must be greater than 0" } * * // Multiple field errors * error.getErrors(); * // { * // "tradingPairId": "Invalid uuid", * // "quantity": "Required", * // "options.timeInForce": "Invalid enum value" * // } * * // Root-level error (no specific field) * error.getErrors(); * // { "root": "At least one field is required" } * * // Use in React form * const errors = validationError.getErrors(); * * ``` */ getErrors(): Record; } /** * Validates data against a Zod schema and throws user-friendly errors on failure. * * This is a convenience wrapper around Zod's `safeParse` that automatically * throws a {@link ValidationError} with formatted error messages when validation fails. * * **When to Use:** * - Validating user input before API calls * - Runtime type checking for dynamic data * - Ensuring data integrity before processing * * **Error Handling:** * On validation failure, throws {@link ValidationError} which provides: * - Human-readable error message * - Structured errors via `getErrors()` * - Raw Zod issues for advanced handling * * @template T - The expected type after validation * @param schema - Zod schema to validate against * @param data - Data to validate (unknown type for safety) * @returns Validated and typed data * @throws {@link ValidationError} When validation fails * * @example * ```typescript * import { validate, PlaceLimitOrderSchema } from '@0xmonaco/types'; * * // Basic validation * try { * const validatedOrder = validate(PlaceLimitOrderSchema, userInput); * // validatedOrder is now typed and guaranteed valid * await sdk.trading.placeLimitOrder(...validatedOrder); * } catch (error) { * if (error instanceof ValidationError) { * // Show user-friendly errors * console.error(error.message); * const fieldErrors = error.getErrors(); * // Display errors next to form fields * } * } * * // With custom error handling * function safeValidate(schema: z.ZodSchema, data: unknown) { * try { * return { success: true, data: validate(schema, data) }; * } catch (error) { * if (error instanceof ValidationError) { * return { success: false, errors: error.getErrors() }; * } * throw error; // Re-throw unexpected errors * } * } * * const result = safeValidate(PlaceLimitOrderSchema, input); * if (result.success) { * // Use result.data * } else { * // Handle result.errors * } * ``` */ export declare function validate(schema: z.ZodSchema, data: unknown): T;