UNPKG

bun-ws-router

Version:

Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.

38 lines 1.3 kB
// SPDX-FileCopyrightText: 2025-present Kriasoft // SPDX-License-Identifier: MIT /** * Formats a Zod validation error into a user-friendly message. * Leverages Zod v4's prettifyError for better error messages. */ export function formatValidationError(error) { if (error && typeof error === "object" && "formatted" in error) { // If we have a prettified error from our adapter const formatted = error.formatted; return JSON.stringify(formatted, null, 2); } // Fallback for raw ZodError if (error && typeof error === "object" && "issues" in error) { const zodError = error; const messages = zodError.issues.map((issue) => { const path = issue.path.join("."); return `${path}: ${issue.message}`; }); return messages.join("\n"); } return "Validation error"; } /** * Extract error context for debugging from a validation error. */ export function getErrorContext(error) { if (error && typeof error === "object" && "issues" in error) { const issues = error .issues; return { errorCount: issues.length, fields: issues.map((issue) => issue.path?.join(".")).filter(Boolean), }; } return {}; } //# sourceMappingURL=utils.js.map