Initial commit: Anvil Lounge chat application
- React frontend with Vite + TypeScript - Cloudflare Worker backend with Durable Objects - AI-powered chat moderation via OpenAI - WebSocket-based real-time messaging - XSS prevention, rate limiting, input validation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
34
worker/src/validation.ts
Normal file
34
worker/src/validation.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
// WebSocket incoming message schemas
|
||||
export const MessageSchema = z.object({
|
||||
type: z.literal('message'),
|
||||
content: z.string().min(1).max(2000).trim(),
|
||||
});
|
||||
|
||||
export const RenameSchema = z.object({
|
||||
type: z.literal('rename'),
|
||||
name: z.string().min(1).max(50).trim(),
|
||||
});
|
||||
|
||||
export const WebSocketMessageSchema = z.discriminatedUnion('type', [
|
||||
MessageSchema,
|
||||
RenameSchema,
|
||||
]);
|
||||
|
||||
// Room ID validation
|
||||
export const RoomIdSchema = z.string()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.regex(/^[a-zA-Z0-9_-]+$/, 'Room ID must be alphanumeric with dashes and underscores only');
|
||||
|
||||
// User name validation
|
||||
export const UserNameSchema = z.string()
|
||||
.min(1)
|
||||
.max(50)
|
||||
.trim();
|
||||
|
||||
// Types
|
||||
export type WebSocketMessage = z.infer<typeof WebSocketMessageSchema>;
|
||||
export type MessageData = z.infer<typeof MessageSchema>;
|
||||
export type RenameData = z.infer<typeof RenameSchema>;
|
||||
Reference in New Issue
Block a user