class ValidationMiddleware { static validateEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } static validateUsername(username) { const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/; return usernameRegex.test(username); } static validatePassword(password) { return password.length >= 6; } static validateRegisterData(data) { const errors = []; if (!data.username) { errors.push('Username is required'); } else if (!this.validateUsername(data.username)) { errors.push('Username must be 3-20 characters and contain only letters, numbers, and underscores'); } if (!data.email) { errors.push('Email is required'); } else if (!this.validateEmail(data.email)) { errors.push('Invalid email format'); } if (!data.password) { errors.push('Password is required'); } else if (!this.validatePassword(data.password)) { errors.push('Password must be at least 6 characters'); } if (data.password !== data.confirmPassword) { errors.push('Passwords do not match'); } return { isValid: errors.length === 0, errors: errors }; } static validateLoginData(data) { const errors = []; if (!data.username) { errors.push('Username is required'); } if (!data.password) { errors.push('Password is required'); } return { isValid: errors.length === 0, errors: errors }; } static validateProfileData(data) { const errors = []; if (data.email && !this.validateEmail(data.email)) { errors.push('Invalid email format'); } if (data.fullName && data.fullName.length < 2) { errors.push('Full name must be at least 2 characters'); } return { isValid: errors.length === 0, errors: errors }; } static sanitizeInput(input) { if (typeof input !== 'string') return input; return input .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/\//g, '/'); } static validateExerciseAnswer(answer) { const errors = []; if (!answer || answer.trim().length === 0) { errors.push('Answer cannot be empty'); } if (answer.length > 10000) { errors.push('Answer is too long'); } return { isValid: errors.length === 0, errors: errors }; } }