export const WsMessageSchema = z.discriminatedUnion("type", [
z.object({ type: z.literal("join_room"), roomId: z.string() }),
z.object({ type: z.literal("chat"), roomId: z.string(), message: z.string() }),
z.object({ type: z.literal("leave_room"), roomId: z.string() }),
6 ]);
Use this in our ws.on('message') handler to ensure every incoming message is perfectly typed and validated before processing.
Current Issue: We use Zod for HTTP requests, but your WebSocket messages are manually parsed and checked with if/else (e.g., parsedData.type === "chat").
Improvement: Define a WebSocket Message Schema.
Use this in our ws.on('message') handler to ensure every incoming message is perfectly typed and validated before processing.