bun-ws-router
Version:
Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.
37 lines • 1.59 kB
JavaScript
// SPDX-FileCopyrightText: 2025-present Kriasoft
// SPDX-License-Identifier: MIT
import * as v from "valibot";
/**
* Adapter that bridges Valibot validation with the generic router implementation.
* Handles the differences in Valibot's schema structure compared to Zod.
*/
export class ValibotValidatorAdapter {
// Extract the literal type value used for message routing.
// COMPLEXITY: Valibot stores the literal value differently than Zod,
// requiring runtime checks to safely access schema.entries.type.literal.
getMessageType(schema) {
const typeSchema = schema.entries.type;
if (typeSchema && typeSchema.type === "literal") {
return typeSchema.literal;
}
// This should never happen with schemas from messageSchema()
throw new Error("Schema must have a literal type field");
}
// Validate incoming message data and normalize the result format.
// NOTE: Valibot uses 'output' instead of Zod's 'data' for parsed results.
// Error format is simpler - just the issues array without prettification.
safeParse(schema, data) {
const result = v.safeParse(schema, data);
return {
success: result.success,
data: result.success ? result.output : undefined,
error: result.success ? undefined : result.issues,
};
}
// Type helper for TypeScript inference - not used at runtime.
// EXISTS FOR: Satisfying the ValidatorAdapter interface type requirements.
infer() {
return {};
}
}
//# sourceMappingURL=adapter.js.map