Skip to content

Unified Validation with Zod #2

Description

@samarkun23

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.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions