Skip to content

Commit

Permalink
add schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc committed Dec 8, 2023
1 parent 84a7a9d commit 01522a3
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/rooms/gateways/rooms.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
import { ZodPipe } from '@/shared/pipes/zod.pipe';
import {
ConnectedSocket,
MessageBody,
SubscribeMessage,
WebSocketGateway,
} from '@nestjs/websockets';
import { Socket } from 'socket.io';
import { z } from 'zod';

const joinRoomSchema = z.string();
const leaveRoomSchema = z.string();

@WebSocketGateway()
export class RoomsGateway {
@SubscribeMessage('join')
join(client: Socket, room: string): string[] {
join(
@ConnectedSocket() client: Socket,
@MessageBody(new ZodPipe(joinRoomSchema))
room: z.infer<typeof joinRoomSchema>,
): string[] {
client.join(room);
return Array.from(client.rooms);
}

@SubscribeMessage('leave')
leave(client: Socket, room: string): string[] {
leave(
@ConnectedSocket() client: Socket,
@MessageBody(new ZodPipe(leaveRoomSchema))
room: z.infer<typeof leaveRoomSchema>,
): string[] {
client.leave(room);
return Array.from(client.rooms);
}
Expand Down

0 comments on commit 01522a3

Please sign in to comment.