Spaces:
Sleeping
Sleeping
File size: 8,707 Bytes
1305386 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | /**
* 🛡️ 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!");
}
}
} |