import Joi from 'joi'; /** * Validation schema for POST /api/slidev/generate request * * Validates: * - sessionId: Must be a valid UUID (v4) * - templateId: Must be 'starter' (expandable for future templates) * - data: Must be an object (flexible key-value pairs for template placeholders) */ export const generateDeckSchema = Joi.object({ sessionId: Joi.string().uuid().required(), templateId: Joi.string().valid('starter').required(), // Expand later with more templates data: Joi.object().required() }); /** * Validates a request body against the generateDeckSchema * * @param body - Request body to validate * @returns Validated and sanitized value * @throws Error if validation fails * * @example * const validatedData = validateGenerateRequest({ * sessionId: '123e4567-e89b-12d3-a456-426614174000', * templateId: 'starter', * data: { COMPANY_NAME: 'Acme Corp' } * }); */ export function validateGenerateRequest(body: any) { const { error, value } = generateDeckSchema.validate(body); if (error) { throw new Error(`Validation failed: ${error.message}`); } return value; } /** * Validation schema for POST /api/slidev/control request * * Validates: * - deckId: Must match format /^[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$/ * - command: Must be 'next', 'prev', or 'goto' * - slideId: Required when command is 'goto', optional otherwise */ export const controlCommandSchema = Joi.object({ deckId: Joi.string() .pattern(/^[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$/) .required() .messages({ 'string.pattern.base': 'deckId must match format: xxxx-xxxx-xxxx (lowercase alphanumeric)' }), command: Joi.string() .valid('next', 'prev', 'goto') .required() .messages({ 'any.only': 'command must be one of: next, prev, goto' }), slideId: Joi.string() .when('command', { is: 'goto', then: Joi.required().messages({ 'any.required': 'slideId is required when command is "goto"' }), otherwise: Joi.optional() }) }); /** * Validates a control command request body * * @param body - Request body to validate * @returns Validated and sanitized value * @throws Error if validation fails * * @example * const validatedData = validateControlRequest({ * deckId: 'a1b2-c3d4-e5f6', * command: 'next' * }); */ export function validateControlRequest(body: any) { const { error, value } = controlCommandSchema.validate(body); if (error) { throw new Error(`Validation failed: ${error.message}`); } return value; }