/** * 🛡️ SERVICE VALIDATOR * * Comprehensive validation system để đảm bảo services được implement đầy đủ. * Giúp phát hiện lỗi implementation sớm thay vì gặp runtime errors. * * Validation checks: * 1. Service config tồn tại trong ServiceConfig.ts * 2. Service implement đầy đủ IService interface * 3. Allocator implement đầy đủ IAllocator interface * 4. PayloadDtoValidator implement đầy đủ IPayloadDtoValidator interface * 5. IPayload interface có tồn tại (warning) * 6. MessageType consistency across all components * * @see interfaces/core/IServiceValidator.ts - Interface definitions */ import { IServiceValidator, ServiceValidationResult, ServiceValidationReport } from '../../interfaces/core/IServiceValidator'; import { IServiceConfig } from '../../interfaces/core/IServiceRegistry'; import { ServiceConfigManager } from '../../config/ServiceConfig'; export class ServiceValidator implements IServiceValidator { /** * Validate một service config đầy đủ */ validateService(config: IServiceConfig): ServiceValidationResult { const errors: string[] = []; const warnings: string[] = []; const serviceType = config.messageType; console.log(`🔍 Validating service: ${serviceType}`); // Chạy tất cả validation checks this.validateServiceConfig(config, errors, warnings); this.validateServiceImplementation(config, errors, warnings); this.validateAllocatorImplementation(config, errors, warnings); this.validatePayloadDtoValidatorImplementation(config, errors, warnings); this.validatePayloadInterface(config, errors, warnings); this.validateConsistency(config, errors, warnings); const isValid = errors.length === 0; if (isValid) { console.log(`✅ Service ${serviceType}: VALID`); } else { console.error(`❌ Service ${serviceType}: INVALID - ${errors.length} error(s)`); } return { isValid, errors, warnings, serviceType }; } private validateServiceConfig(config: IServiceConfig, errors: string[], warnings: string[]): void { // Kiểm tra có config trong ServiceConfig.ts không const serviceConfig = ServiceConfigManager.getServiceConfig(config.messageType); if (!serviceConfig) { errors.push(`Service config not found in ServiceConfig.ts for messageType: ${config.messageType}`); return; } // Kiểm tra consistency với ServiceConfig.ts if (config.typeConfig.messageType !== serviceConfig.messageType) { errors.push(`MessageType mismatch: factory=${config.typeConfig.messageType}, config=${serviceConfig.messageType}`); } // Validate config structure if (!ServiceConfigManager.validateServiceConfig(serviceConfig)) { errors.push(`Invalid service configuration structure for ${config.messageType}`); } // Warnings cho performance concerns if (serviceConfig.max > 10) { warnings.push(`High max instances (${serviceConfig.max}) may cause resource contention`); } } private validateServiceImplementation(config: IServiceConfig, errors: string[], warnings: string[]): void { if (!config.service) { errors.push("Service implementation is missing"); return; } // Kiểm tra có implement IService interface if (!config.service.messageType) { errors.push("Service.messageType is missing"); } if (typeof config.service.handle !== 'function') { errors.push("Service.handle method is missing or not a function"); } // Kiểm tra messageType consistency if (config.service.messageType !== config.messageType) { errors.push(`Service messageType mismatch: expected=${config.messageType}, actual=${config.service.messageType}`); } } private validateAllocatorImplementation(config: IServiceConfig, errors: string[], warnings: string[]): void { if (!config.allocator) { errors.push("Allocator implementation is missing"); return; } // Kiểm tra có implement IAllocator interface if (!config.allocator.messageType) { errors.push("Allocator.messageType is missing"); } if (typeof config.allocator.allocate !== 'function') { errors.push("Allocator.allocate method is missing or not a function"); } // Kiểm tra messageType consistency if (config.allocator.messageType !== config.messageType) { errors.push(`Allocator messageType mismatch: expected=${config.messageType}, actual=${config.allocator.messageType}`); } } private validatePayloadDtoValidatorImplementation(config: IServiceConfig, errors: string[], warnings: string[]): void { if (!config.payloadDtoValidator) { errors.push("PayloadDtoValidator implementation is missing"); return; } // Kiểm tra có implement IPayloadDtoValidator interface if (typeof config.payloadDtoValidator.validate !== 'function') { errors.push("PayloadDtoValidator.validate method is missing or not a function"); } } private validatePayloadInterface(config: IServiceConfig, errors: string[], warnings: string[]): void { // Không thể check interface tại runtime, nhưng có thể check naming convention const expectedInterfacePath = `interfaces/services/${config.messageType}/IPayload.ts`; warnings.push(`Ensure IPayload interface exists at: src/${expectedInterfacePath}`); } private validateConsistency(config: IServiceConfig, errors: string[], warnings: string[]): void { // Kiểm tra tất cả components có cùng messageType const messageTypes = [ config.messageType, config.service?.messageType, config.allocator?.messageType, config.typeConfig.messageType ].filter(Boolean); const uniqueMessageTypes = [...new Set(messageTypes)]; if (uniqueMessageTypes.length > 1) { errors.push(`Inconsistent messageTypes across components: ${uniqueMessageTypes.join(', ')}`); } } /** * Validate tất cả services và tạo report tổng hợp */ validateAllServices(configs: IServiceConfig[]): ServiceValidationReport { console.log("🔍 Starting comprehensive service validation..."); console.log("=".repeat(60)); const results = configs.map(config => this.validateService(config)); const report: ServiceValidationReport = { totalServices: configs.length, validServices: results.filter(r => r.isValid).length, invalidServices: results.filter(r => !r.isValid).length, results }; this.printValidationReport(report); return report; } private printValidationReport(report: ServiceValidationReport): void { console.log("\n📊 SERVICE VALIDATION REPORT:"); console.log("=".repeat(60)); console.log(`Total Services: ${report.totalServices}`); console.log(`✅ Valid: ${report.validServices}`); console.log(`❌ Invalid: ${report.invalidServices}`); console.log(); report.results.forEach((result, index) => { const status = result.isValid ? "✅ VALID" : "❌ INVALID"; console.log(`${index + 1}. ${result.serviceType}: ${status}`); if (result.errors.length > 0) { console.log(` Errors:`); result.errors.forEach(error => console.log(` - ${error}`)); } if (result.warnings.length > 0) { console.log(` Warnings:`); result.warnings.forEach(warning => console.log(` - ${warning}`)); } console.log(); }); if (report.invalidServices > 0) { console.error(`🚨 CRITICAL: ${report.invalidServices} services are invalid and may cause runtime errors!`); console.error("Please fix the errors before deploying."); } else { console.log("🎉 All services passed validation!"); } } }