Files
chat-app/worker/src/validation.ts
kappa 554c578345 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>
2026-01-19 10:17:27 +09:00

35 lines
877 B
TypeScript

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>;