@morojs/moro 1.5.4 → 1.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/core/config/config-manager.d.ts +44 -0
  2. package/dist/core/config/config-manager.js +114 -0
  3. package/dist/core/config/config-manager.js.map +1 -0
  4. package/dist/core/config/config-sources.d.ts +21 -0
  5. package/dist/core/config/config-sources.js +314 -0
  6. package/dist/core/config/config-sources.js.map +1 -0
  7. package/dist/core/config/config-validator.d.ts +21 -0
  8. package/dist/core/config/config-validator.js +737 -0
  9. package/dist/core/config/config-validator.js.map +1 -0
  10. package/dist/core/config/file-loader.d.ts +0 -5
  11. package/dist/core/config/file-loader.js +0 -171
  12. package/dist/core/config/file-loader.js.map +1 -1
  13. package/dist/core/config/index.d.ts +39 -10
  14. package/dist/core/config/index.js +66 -29
  15. package/dist/core/config/index.js.map +1 -1
  16. package/dist/core/config/schema.js +22 -31
  17. package/dist/core/config/schema.js.map +1 -1
  18. package/dist/core/config/utils.d.ts +9 -2
  19. package/dist/core/config/utils.js +19 -32
  20. package/dist/core/config/utils.js.map +1 -1
  21. package/dist/core/framework.d.ts +2 -7
  22. package/dist/core/framework.js +12 -5
  23. package/dist/core/framework.js.map +1 -1
  24. package/dist/core/http/http-server.d.ts +12 -0
  25. package/dist/core/http/http-server.js +56 -0
  26. package/dist/core/http/http-server.js.map +1 -1
  27. package/dist/core/http/router.d.ts +12 -0
  28. package/dist/core/http/router.js +114 -36
  29. package/dist/core/http/router.js.map +1 -1
  30. package/dist/core/logger/index.d.ts +1 -1
  31. package/dist/core/logger/index.js +2 -1
  32. package/dist/core/logger/index.js.map +1 -1
  33. package/dist/core/logger/logger.d.ts +10 -1
  34. package/dist/core/logger/logger.js +99 -37
  35. package/dist/core/logger/logger.js.map +1 -1
  36. package/dist/core/routing/index.d.ts +20 -0
  37. package/dist/core/routing/index.js +109 -11
  38. package/dist/core/routing/index.js.map +1 -1
  39. package/dist/moro.d.ts +22 -0
  40. package/dist/moro.js +134 -98
  41. package/dist/moro.js.map +1 -1
  42. package/dist/types/config.d.ts +39 -2
  43. package/dist/types/core.d.ts +22 -39
  44. package/dist/types/logger.d.ts +4 -0
  45. package/package.json +1 -1
  46. package/src/core/config/config-manager.ts +133 -0
  47. package/src/core/config/config-sources.ts +384 -0
  48. package/src/core/config/config-validator.ts +1035 -0
  49. package/src/core/config/file-loader.ts +0 -233
  50. package/src/core/config/index.ts +77 -32
  51. package/src/core/config/schema.ts +22 -31
  52. package/src/core/config/utils.ts +22 -29
  53. package/src/core/framework.ts +18 -11
  54. package/src/core/http/http-server.ts +66 -0
  55. package/src/core/http/router.ts +127 -38
  56. package/src/core/logger/index.ts +1 -0
  57. package/src/core/logger/logger.ts +109 -36
  58. package/src/core/routing/index.ts +116 -12
  59. package/src/moro.ts +159 -107
  60. package/src/types/config.ts +40 -2
  61. package/src/types/core.ts +32 -43
  62. package/src/types/logger.ts +6 -0
  63. package/dist/core/config/loader.d.ts +0 -7
  64. package/dist/core/config/loader.js +0 -269
  65. package/dist/core/config/loader.js.map +0 -1
  66. package/dist/core/config/validation.d.ts +0 -17
  67. package/dist/core/config/validation.js +0 -131
  68. package/dist/core/config/validation.js.map +0 -1
  69. package/src/core/config/loader.ts +0 -633
  70. package/src/core/config/validation.ts +0 -140
@@ -0,0 +1,737 @@
1
+ "use strict";
2
+ /**
3
+ * Configuration Validator - Type-Safe Schema Validation
4
+ *
5
+ * This module provides runtime validation for configuration objects using
6
+ * simple TypeScript functions that match the type definitions exactly.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ConfigValidationError = void 0;
10
+ exports.validateConfig = validateConfig;
11
+ const logger_1 = require("../logger");
12
+ const logger = (0, logger_1.createFrameworkLogger)('ConfigValidator');
13
+ /**
14
+ * Configuration validation error with detailed context
15
+ */
16
+ class ConfigValidationError extends Error {
17
+ field;
18
+ value;
19
+ expectedType;
20
+ constructor(field, value, expectedType, message) {
21
+ super(`Configuration validation failed for '${field}': ${message}`);
22
+ this.field = field;
23
+ this.value = value;
24
+ this.expectedType = expectedType;
25
+ this.name = 'ConfigValidationError';
26
+ }
27
+ }
28
+ exports.ConfigValidationError = ConfigValidationError;
29
+ /**
30
+ * Validate and normalize a complete configuration object
31
+ * This ensures type safety and provides helpful error messages
32
+ */
33
+ function validateConfig(config) {
34
+ logger.debug('Validating configuration');
35
+ try {
36
+ const validatedConfig = {
37
+ server: validateServerConfig(config.server, 'server'),
38
+ serviceDiscovery: validateServiceDiscoveryConfig(config.serviceDiscovery, 'serviceDiscovery'),
39
+ database: validateDatabaseConfig(config.database, 'database'),
40
+ modules: validateModuleDefaultsConfig(config.modules, 'modules'),
41
+ logging: validateLoggingConfig(config.logging, 'logging'),
42
+ security: validateSecurityConfig(config.security, 'security'),
43
+ external: validateExternalServicesConfig(config.external, 'external'),
44
+ performance: validatePerformanceConfig(config.performance, 'performance'),
45
+ websocket: validateWebSocketConfig(config.websocket, 'websocket'),
46
+ };
47
+ logger.debug('Configuration validation successful');
48
+ return validatedConfig;
49
+ }
50
+ catch (error) {
51
+ if (error instanceof ConfigValidationError) {
52
+ logger.error(`❌ Configuration validation failed for '${error.field}':`, error.message);
53
+ // Provide helpful hints
54
+ provideValidationHints(error);
55
+ throw error;
56
+ }
57
+ logger.error('❌ Unexpected configuration validation error:', String(error));
58
+ throw new Error(`Configuration validation failed: ${String(error)}`);
59
+ }
60
+ }
61
+ /**
62
+ * Validate server configuration
63
+ */
64
+ function validateServerConfig(config, path) {
65
+ if (!config || typeof config !== 'object') {
66
+ throw new ConfigValidationError(path, config, 'object', 'Server configuration must be an object');
67
+ }
68
+ return {
69
+ port: validatePort(config.port, `${path}.port`),
70
+ host: validateString(config.host, `${path}.host`),
71
+ maxConnections: validateNumber(config.maxConnections, `${path}.maxConnections`, { min: 1 }),
72
+ timeout: validateNumber(config.timeout, `${path}.timeout`, { min: 1000 }),
73
+ };
74
+ }
75
+ /**
76
+ * Validate service discovery configuration
77
+ */
78
+ function validateServiceDiscoveryConfig(config, path) {
79
+ if (!config || typeof config !== 'object') {
80
+ throw new ConfigValidationError(path, config, 'object', 'Service discovery configuration must be an object');
81
+ }
82
+ return {
83
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
84
+ type: validateEnum(config.type, ['memory', 'consul', 'kubernetes'], `${path}.type`),
85
+ consulUrl: validateString(config.consulUrl, `${path}.consulUrl`),
86
+ kubernetesNamespace: validateString(config.kubernetesNamespace, `${path}.kubernetesNamespace`),
87
+ healthCheckInterval: validateNumber(config.healthCheckInterval, `${path}.healthCheckInterval`, {
88
+ min: 1000,
89
+ }),
90
+ retryAttempts: validateNumber(config.retryAttempts, `${path}.retryAttempts`, { min: 0 }),
91
+ };
92
+ }
93
+ /**
94
+ * Validate database configuration
95
+ */
96
+ function validateDatabaseConfig(config, path) {
97
+ if (!config || typeof config !== 'object') {
98
+ throw new ConfigValidationError(path, config, 'object', 'Database configuration must be an object');
99
+ }
100
+ const result = {};
101
+ // Optional URL
102
+ if (config.url !== undefined) {
103
+ result.url = validateString(config.url, `${path}.url`);
104
+ }
105
+ // Optional Redis - only validate if present
106
+ if (config.redis !== undefined) {
107
+ result.redis = validateRedisConfig(config.redis, `${path}.redis`);
108
+ }
109
+ // Optional MySQL - only validate if present
110
+ if (config.mysql !== undefined) {
111
+ result.mysql = validateMySQLConfig(config.mysql, `${path}.mysql`);
112
+ }
113
+ // Optional PostgreSQL - only validate if present
114
+ if (config.postgresql !== undefined) {
115
+ result.postgresql = validatePostgreSQLConfig(config.postgresql, `${path}.postgresql`);
116
+ }
117
+ // Optional SQLite - only validate if present
118
+ if (config.sqlite !== undefined) {
119
+ result.sqlite = validateSQLiteConfig(config.sqlite, `${path}.sqlite`);
120
+ }
121
+ // Optional MongoDB - only validate if present
122
+ if (config.mongodb !== undefined) {
123
+ result.mongodb = validateMongoDBConfig(config.mongodb, `${path}.mongodb`);
124
+ }
125
+ return result;
126
+ }
127
+ /**
128
+ * Validate Redis configuration
129
+ */
130
+ function validateRedisConfig(config, path) {
131
+ if (!config || typeof config !== 'object') {
132
+ throw new ConfigValidationError(path, config, 'object', 'Redis configuration must be an object');
133
+ }
134
+ return {
135
+ url: validateString(config.url, `${path}.url`),
136
+ maxRetries: validateNumber(config.maxRetries, `${path}.maxRetries`, { min: 0 }),
137
+ retryDelay: validateNumber(config.retryDelay, `${path}.retryDelay`, { min: 0 }),
138
+ keyPrefix: validateString(config.keyPrefix, `${path}.keyPrefix`),
139
+ };
140
+ }
141
+ /**
142
+ * Validate MySQL configuration
143
+ */
144
+ function validateMySQLConfig(config, path) {
145
+ if (!config || typeof config !== 'object') {
146
+ throw new ConfigValidationError(path, config, 'object', 'MySQL configuration must be an object');
147
+ }
148
+ const result = {
149
+ host: validateString(config.host, `${path}.host`),
150
+ port: validatePort(config.port, `${path}.port`),
151
+ connectionLimit: validateNumber(config.connectionLimit, `${path}.connectionLimit`, { min: 1 }),
152
+ acquireTimeout: validateNumber(config.acquireTimeout, `${path}.acquireTimeout`, { min: 1000 }),
153
+ timeout: validateNumber(config.timeout, `${path}.timeout`, { min: 1000 }),
154
+ };
155
+ // Optional fields
156
+ if (config.database !== undefined) {
157
+ result.database = validateString(config.database, `${path}.database`);
158
+ }
159
+ if (config.username !== undefined) {
160
+ result.username = validateString(config.username, `${path}.username`);
161
+ }
162
+ if (config.password !== undefined) {
163
+ result.password = validateString(config.password, `${path}.password`);
164
+ }
165
+ return result;
166
+ }
167
+ /**
168
+ * Validate PostgreSQL configuration
169
+ */
170
+ function validatePostgreSQLConfig(config, path) {
171
+ if (!config || typeof config !== 'object') {
172
+ throw new ConfigValidationError(path, config, 'object', 'PostgreSQL configuration must be an object');
173
+ }
174
+ const result = {
175
+ host: validateString(config.host, `${path}.host`),
176
+ port: validatePort(config.port, `${path}.port`),
177
+ connectionLimit: validateNumber(config.connectionLimit, `${path}.connectionLimit`, { min: 1 }),
178
+ };
179
+ // Optional fields
180
+ if (config.database !== undefined) {
181
+ result.database = validateString(config.database, `${path}.database`);
182
+ }
183
+ if (config.user !== undefined) {
184
+ result.user = validateString(config.user, `${path}.user`);
185
+ }
186
+ if (config.password !== undefined) {
187
+ result.password = validateString(config.password, `${path}.password`);
188
+ }
189
+ if (config.ssl !== undefined) {
190
+ result.ssl = validateBoolean(config.ssl, `${path}.ssl`);
191
+ }
192
+ return result;
193
+ }
194
+ /**
195
+ * Validate SQLite configuration
196
+ */
197
+ function validateSQLiteConfig(config, path) {
198
+ if (!config || typeof config !== 'object') {
199
+ throw new ConfigValidationError(path, config, 'object', 'SQLite configuration must be an object');
200
+ }
201
+ const result = {
202
+ filename: validateString(config.filename, `${path}.filename`),
203
+ };
204
+ // Optional fields
205
+ if (config.memory !== undefined) {
206
+ result.memory = validateBoolean(config.memory, `${path}.memory`);
207
+ }
208
+ if (config.verbose !== undefined) {
209
+ result.verbose = validateBoolean(config.verbose, `${path}.verbose`);
210
+ }
211
+ return result;
212
+ }
213
+ /**
214
+ * Validate MongoDB configuration
215
+ */
216
+ function validateMongoDBConfig(config, path) {
217
+ if (!config || typeof config !== 'object') {
218
+ throw new ConfigValidationError(path, config, 'object', 'MongoDB configuration must be an object');
219
+ }
220
+ const result = {};
221
+ // Either url or host+port
222
+ if (config.url !== undefined) {
223
+ result.url = validateString(config.url, `${path}.url`);
224
+ }
225
+ if (config.host !== undefined) {
226
+ result.host = validateString(config.host, `${path}.host`);
227
+ }
228
+ if (config.port !== undefined) {
229
+ result.port = validatePort(config.port, `${path}.port`);
230
+ }
231
+ if (config.database !== undefined) {
232
+ result.database = validateString(config.database, `${path}.database`);
233
+ }
234
+ if (config.username !== undefined) {
235
+ result.username = validateString(config.username, `${path}.username`);
236
+ }
237
+ if (config.password !== undefined) {
238
+ result.password = validateString(config.password, `${path}.password`);
239
+ }
240
+ return result;
241
+ }
242
+ /**
243
+ * Validate module defaults configuration
244
+ */
245
+ function validateModuleDefaultsConfig(config, path) {
246
+ if (!config || typeof config !== 'object') {
247
+ throw new ConfigValidationError(path, config, 'object', 'Module defaults configuration must be an object');
248
+ }
249
+ return {
250
+ cache: validateCacheConfig(config.cache, `${path}.cache`),
251
+ rateLimit: validateRateLimitConfig(config.rateLimit, `${path}.rateLimit`),
252
+ validation: validateValidationConfig(config.validation, `${path}.validation`),
253
+ };
254
+ }
255
+ /**
256
+ * Validate cache configuration
257
+ */
258
+ function validateCacheConfig(config, path) {
259
+ if (!config || typeof config !== 'object') {
260
+ throw new ConfigValidationError(path, config, 'object', 'Cache configuration must be an object');
261
+ }
262
+ return {
263
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
264
+ defaultTtl: validateNumber(config.defaultTtl, `${path}.defaultTtl`, { min: 0 }),
265
+ maxSize: validateNumber(config.maxSize, `${path}.maxSize`, { min: 1 }),
266
+ strategy: validateEnum(config.strategy, ['lru', 'lfu', 'fifo'], `${path}.strategy`),
267
+ };
268
+ }
269
+ /**
270
+ * Validate rate limit configuration
271
+ */
272
+ function validateRateLimitConfig(config, path) {
273
+ if (!config || typeof config !== 'object') {
274
+ throw new ConfigValidationError(path, config, 'object', 'Rate limit configuration must be an object');
275
+ }
276
+ return {
277
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
278
+ defaultRequests: validateNumber(config.defaultRequests, `${path}.defaultRequests`, { min: 1 }),
279
+ defaultWindow: validateNumber(config.defaultWindow, `${path}.defaultWindow`, { min: 1000 }),
280
+ skipSuccessfulRequests: validateBoolean(config.skipSuccessfulRequests, `${path}.skipSuccessfulRequests`),
281
+ skipFailedRequests: validateBoolean(config.skipFailedRequests, `${path}.skipFailedRequests`),
282
+ };
283
+ }
284
+ /**
285
+ * Validate validation configuration
286
+ */
287
+ function validateValidationConfig(config, path) {
288
+ if (!config || typeof config !== 'object') {
289
+ throw new ConfigValidationError(path, config, 'object', 'Validation configuration must be an object');
290
+ }
291
+ return {
292
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
293
+ stripUnknown: validateBoolean(config.stripUnknown, `${path}.stripUnknown`),
294
+ abortEarly: validateBoolean(config.abortEarly, `${path}.abortEarly`),
295
+ };
296
+ }
297
+ /**
298
+ * Validate logging configuration
299
+ */
300
+ function validateLoggingConfig(config, path) {
301
+ if (!config || typeof config !== 'object') {
302
+ throw new ConfigValidationError(path, config, 'object', 'Logging configuration must be an object');
303
+ }
304
+ return {
305
+ level: validateEnum(config.level, ['debug', 'info', 'warn', 'error', 'fatal'], `${path}.level`),
306
+ format: validateEnum(config.format, ['pretty', 'json', 'compact'], `${path}.format`),
307
+ enableColors: validateBoolean(config.enableColors, `${path}.enableColors`),
308
+ enableTimestamp: validateBoolean(config.enableTimestamp, `${path}.enableTimestamp`),
309
+ enableContext: validateBoolean(config.enableContext, `${path}.enableContext`),
310
+ outputs: validateLoggingOutputsConfig(config.outputs, `${path}.outputs`),
311
+ };
312
+ }
313
+ /**
314
+ * Validate logging outputs configuration
315
+ */
316
+ function validateLoggingOutputsConfig(config, path) {
317
+ if (!config || typeof config !== 'object') {
318
+ throw new ConfigValidationError(path, config, 'object', 'Logging outputs configuration must be an object');
319
+ }
320
+ return {
321
+ console: validateBoolean(config.console, `${path}.console`),
322
+ file: validateLoggingFileConfig(config.file, `${path}.file`),
323
+ webhook: validateLoggingWebhookConfig(config.webhook, `${path}.webhook`),
324
+ };
325
+ }
326
+ /**
327
+ * Validate logging file configuration
328
+ */
329
+ function validateLoggingFileConfig(config, path) {
330
+ if (!config || typeof config !== 'object') {
331
+ throw new ConfigValidationError(path, config, 'object', 'Logging file configuration must be an object');
332
+ }
333
+ return {
334
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
335
+ path: validateString(config.path, `${path}.path`),
336
+ maxSize: validateString(config.maxSize, `${path}.maxSize`),
337
+ maxFiles: validateNumber(config.maxFiles, `${path}.maxFiles`, { min: 1 }),
338
+ };
339
+ }
340
+ /**
341
+ * Validate logging webhook configuration
342
+ */
343
+ function validateLoggingWebhookConfig(config, path) {
344
+ if (!config || typeof config !== 'object') {
345
+ throw new ConfigValidationError(path, config, 'object', 'Logging webhook configuration must be an object');
346
+ }
347
+ const result = {
348
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
349
+ headers: validateObject(config.headers, `${path}.headers`),
350
+ };
351
+ // Optional URL
352
+ if (config.url !== undefined) {
353
+ result.url = validateString(config.url, `${path}.url`);
354
+ }
355
+ return result;
356
+ }
357
+ /**
358
+ * Validate security configuration
359
+ */
360
+ function validateSecurityConfig(config, path) {
361
+ if (!config || typeof config !== 'object') {
362
+ throw new ConfigValidationError(path, config, 'object', 'Security configuration must be an object');
363
+ }
364
+ return {
365
+ cors: validateCorsConfig(config.cors, `${path}.cors`),
366
+ helmet: validateHelmetConfig(config.helmet, `${path}.helmet`),
367
+ rateLimit: validateSecurityRateLimitConfig(config.rateLimit, `${path}.rateLimit`),
368
+ };
369
+ }
370
+ /**
371
+ * Validate CORS configuration
372
+ */
373
+ function validateCorsConfig(config, path) {
374
+ if (!config || typeof config !== 'object') {
375
+ throw new ConfigValidationError(path, config, 'object', 'CORS configuration must be an object');
376
+ }
377
+ return {
378
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
379
+ origin: validateCorsOrigin(config.origin, `${path}.origin`),
380
+ methods: validateStringArray(config.methods, `${path}.methods`),
381
+ allowedHeaders: validateStringArray(config.allowedHeaders, `${path}.allowedHeaders`),
382
+ credentials: validateBoolean(config.credentials, `${path}.credentials`),
383
+ };
384
+ }
385
+ /**
386
+ * Validate CORS origin (can be string, array, or boolean)
387
+ */
388
+ function validateCorsOrigin(value, path) {
389
+ if (typeof value === 'boolean' || typeof value === 'string') {
390
+ return value;
391
+ }
392
+ if (Array.isArray(value)) {
393
+ return validateStringArray(value, path);
394
+ }
395
+ throw new ConfigValidationError(path, value, 'string | string[] | boolean', 'Must be a string, array of strings, or boolean');
396
+ }
397
+ /**
398
+ * Validate helmet configuration
399
+ */
400
+ function validateHelmetConfig(config, path) {
401
+ if (!config || typeof config !== 'object') {
402
+ throw new ConfigValidationError(path, config, 'object', 'Helmet configuration must be an object');
403
+ }
404
+ return {
405
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
406
+ contentSecurityPolicy: validateBoolean(config.contentSecurityPolicy, `${path}.contentSecurityPolicy`),
407
+ hsts: validateBoolean(config.hsts, `${path}.hsts`),
408
+ noSniff: validateBoolean(config.noSniff, `${path}.noSniff`),
409
+ frameguard: validateBoolean(config.frameguard, `${path}.frameguard`),
410
+ };
411
+ }
412
+ /**
413
+ * Validate security rate limit configuration
414
+ */
415
+ function validateSecurityRateLimitConfig(config, path) {
416
+ if (!config || typeof config !== 'object') {
417
+ throw new ConfigValidationError(path, config, 'object', 'Security rate limit configuration must be an object');
418
+ }
419
+ return {
420
+ global: validateGlobalRateLimitConfig(config.global, `${path}.global`),
421
+ };
422
+ }
423
+ /**
424
+ * Validate global rate limit configuration
425
+ */
426
+ function validateGlobalRateLimitConfig(config, path) {
427
+ if (!config || typeof config !== 'object') {
428
+ throw new ConfigValidationError(path, config, 'object', 'Global rate limit configuration must be an object');
429
+ }
430
+ return {
431
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
432
+ requests: validateNumber(config.requests, `${path}.requests`, { min: 1 }),
433
+ window: validateNumber(config.window, `${path}.window`, { min: 1000 }),
434
+ };
435
+ }
436
+ /**
437
+ * Validate external services configuration
438
+ */
439
+ function validateExternalServicesConfig(config, path) {
440
+ if (!config || typeof config !== 'object') {
441
+ throw new ConfigValidationError(path, config, 'object', 'External services configuration must be an object');
442
+ }
443
+ const result = {};
444
+ // Optional services - only validate if present
445
+ if (config.stripe !== undefined) {
446
+ result.stripe = validateStripeConfig(config.stripe, `${path}.stripe`);
447
+ }
448
+ if (config.paypal !== undefined) {
449
+ result.paypal = validatePayPalConfig(config.paypal, `${path}.paypal`);
450
+ }
451
+ if (config.smtp !== undefined) {
452
+ result.smtp = validateSMTPConfig(config.smtp, `${path}.smtp`);
453
+ }
454
+ return result;
455
+ }
456
+ /**
457
+ * Validate Stripe configuration
458
+ */
459
+ function validateStripeConfig(config, path) {
460
+ if (!config || typeof config !== 'object') {
461
+ throw new ConfigValidationError(path, config, 'object', 'Stripe configuration must be an object');
462
+ }
463
+ const result = {};
464
+ // Optional fields
465
+ if (config.secretKey !== undefined) {
466
+ result.secretKey = validateString(config.secretKey, `${path}.secretKey`);
467
+ }
468
+ if (config.publishableKey !== undefined) {
469
+ result.publishableKey = validateString(config.publishableKey, `${path}.publishableKey`);
470
+ }
471
+ if (config.webhookSecret !== undefined) {
472
+ result.webhookSecret = validateString(config.webhookSecret, `${path}.webhookSecret`);
473
+ }
474
+ if (config.apiVersion !== undefined) {
475
+ result.apiVersion = validateString(config.apiVersion, `${path}.apiVersion`);
476
+ }
477
+ else {
478
+ result.apiVersion = '2023-10-16'; // Default API version
479
+ }
480
+ return result;
481
+ }
482
+ /**
483
+ * Validate PayPal configuration
484
+ */
485
+ function validatePayPalConfig(config, path) {
486
+ if (!config || typeof config !== 'object') {
487
+ throw new ConfigValidationError(path, config, 'object', 'PayPal configuration must be an object');
488
+ }
489
+ const result = {
490
+ environment: validateEnum(config.environment, ['sandbox', 'production'], `${path}.environment`),
491
+ };
492
+ // Optional fields
493
+ if (config.clientId !== undefined) {
494
+ result.clientId = validateString(config.clientId, `${path}.clientId`);
495
+ }
496
+ if (config.clientSecret !== undefined) {
497
+ result.clientSecret = validateString(config.clientSecret, `${path}.clientSecret`);
498
+ }
499
+ if (config.webhookId !== undefined) {
500
+ result.webhookId = validateString(config.webhookId, `${path}.webhookId`);
501
+ }
502
+ return result;
503
+ }
504
+ /**
505
+ * Validate SMTP configuration
506
+ */
507
+ function validateSMTPConfig(config, path) {
508
+ if (!config || typeof config !== 'object') {
509
+ throw new ConfigValidationError(path, config, 'object', 'SMTP configuration must be an object');
510
+ }
511
+ const result = {
512
+ port: validatePort(config.port, `${path}.port`),
513
+ secure: validateBoolean(config.secure, `${path}.secure`),
514
+ };
515
+ // Optional fields
516
+ if (config.host !== undefined) {
517
+ result.host = validateString(config.host, `${path}.host`);
518
+ }
519
+ if (config.username !== undefined) {
520
+ result.username = validateString(config.username, `${path}.username`);
521
+ }
522
+ if (config.password !== undefined) {
523
+ result.password = validateString(config.password, `${path}.password`);
524
+ }
525
+ return result;
526
+ }
527
+ /**
528
+ * Validate performance configuration
529
+ */
530
+ function validatePerformanceConfig(config, path) {
531
+ if (!config || typeof config !== 'object') {
532
+ throw new ConfigValidationError(path, config, 'object', 'Performance configuration must be an object');
533
+ }
534
+ return {
535
+ compression: validateCompressionConfig(config.compression, `${path}.compression`),
536
+ circuitBreaker: validateCircuitBreakerConfig(config.circuitBreaker, `${path}.circuitBreaker`),
537
+ clustering: validateClusteringConfig(config.clustering, `${path}.clustering`),
538
+ };
539
+ }
540
+ /**
541
+ * Validate compression configuration
542
+ */
543
+ function validateCompressionConfig(config, path) {
544
+ if (!config || typeof config !== 'object') {
545
+ throw new ConfigValidationError(path, config, 'object', 'Compression configuration must be an object');
546
+ }
547
+ return {
548
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
549
+ level: validateNumber(config.level, `${path}.level`, { min: 1, max: 9 }),
550
+ threshold: validateNumber(config.threshold, `${path}.threshold`, { min: 0 }),
551
+ };
552
+ }
553
+ /**
554
+ * Validate circuit breaker configuration
555
+ */
556
+ function validateCircuitBreakerConfig(config, path) {
557
+ if (!config || typeof config !== 'object') {
558
+ throw new ConfigValidationError(path, config, 'object', 'Circuit breaker configuration must be an object');
559
+ }
560
+ return {
561
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
562
+ failureThreshold: validateNumber(config.failureThreshold, `${path}.failureThreshold`, {
563
+ min: 1,
564
+ }),
565
+ resetTimeout: validateNumber(config.resetTimeout, `${path}.resetTimeout`, { min: 1000 }),
566
+ monitoringPeriod: validateNumber(config.monitoringPeriod, `${path}.monitoringPeriod`, {
567
+ min: 1000,
568
+ }),
569
+ };
570
+ }
571
+ /**
572
+ * Validate clustering configuration
573
+ */
574
+ function validateClusteringConfig(config, path) {
575
+ if (!config || typeof config !== 'object') {
576
+ throw new ConfigValidationError(path, config, 'object', 'Clustering configuration must be an object');
577
+ }
578
+ const result = {
579
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
580
+ };
581
+ // Workers can be number or 'auto'
582
+ if (typeof config.workers === 'string' && config.workers === 'auto') {
583
+ result.workers = 'auto';
584
+ }
585
+ else {
586
+ result.workers = validateNumber(config.workers, `${path}.workers`, { min: 1 });
587
+ }
588
+ // Optional memoryPerWorkerGB
589
+ if (config.memoryPerWorkerGB !== undefined) {
590
+ result.memoryPerWorkerGB = validateNumber(config.memoryPerWorkerGB, `${path}.memoryPerWorkerGB`, { min: 0.1 });
591
+ }
592
+ return result;
593
+ }
594
+ /**
595
+ * Validate WebSocket configuration
596
+ */
597
+ function validateWebSocketConfig(config, path) {
598
+ if (!config || typeof config !== 'object') {
599
+ throw new ConfigValidationError(path, config, 'object', 'WebSocket configuration must be an object');
600
+ }
601
+ const result = {
602
+ enabled: validateBoolean(config.enabled, `${path}.enabled`),
603
+ };
604
+ // Optional fields
605
+ if (config.adapter !== undefined) {
606
+ result.adapter = validateString(config.adapter, `${path}.adapter`);
607
+ }
608
+ if (config.compression !== undefined) {
609
+ result.compression = validateBoolean(config.compression, `${path}.compression`);
610
+ }
611
+ if (config.customIdGenerator !== undefined) {
612
+ result.customIdGenerator = config.customIdGenerator; // Function - no validation needed
613
+ }
614
+ if (config.options !== undefined) {
615
+ result.options = validateWebSocketOptions(config.options, `${path}.options`);
616
+ }
617
+ return result;
618
+ }
619
+ /**
620
+ * Validate WebSocket options
621
+ */
622
+ function validateWebSocketOptions(config, path) {
623
+ if (!config || typeof config !== 'object') {
624
+ throw new ConfigValidationError(path, config, 'object', 'WebSocket options must be an object');
625
+ }
626
+ const result = {};
627
+ if (config.cors !== undefined) {
628
+ result.cors = validateWebSocketCorsOptions(config.cors, `${path}.cors`);
629
+ }
630
+ if (config.path !== undefined) {
631
+ result.path = validateString(config.path, `${path}.path`);
632
+ }
633
+ if (config.maxPayloadLength !== undefined) {
634
+ result.maxPayloadLength = validateNumber(config.maxPayloadLength, `${path}.maxPayloadLength`, {
635
+ min: 1024,
636
+ });
637
+ }
638
+ return result;
639
+ }
640
+ /**
641
+ * Validate WebSocket CORS options
642
+ */
643
+ function validateWebSocketCorsOptions(config, path) {
644
+ if (!config || typeof config !== 'object') {
645
+ throw new ConfigValidationError(path, config, 'object', 'WebSocket CORS options must be an object');
646
+ }
647
+ const result = {};
648
+ if (config.origin !== undefined) {
649
+ result.origin = validateCorsOrigin(config.origin, `${path}.origin`);
650
+ }
651
+ if (config.credentials !== undefined) {
652
+ result.credentials = validateBoolean(config.credentials, `${path}.credentials`);
653
+ }
654
+ return result;
655
+ }
656
+ // Basic validation functions
657
+ function validatePort(value, path) {
658
+ const num = Number(value);
659
+ if (isNaN(num) || num < 1 || num > 65535) {
660
+ throw new ConfigValidationError(path, value, 'number (1-65535)', 'Must be a number between 1 and 65535');
661
+ }
662
+ return num;
663
+ }
664
+ function validateBoolean(value, path) {
665
+ if (value === 'true' || value === true)
666
+ return true;
667
+ if (value === 'false' || value === false)
668
+ return false;
669
+ if (value === '1' || value === 1)
670
+ return true;
671
+ if (value === '0' || value === 0)
672
+ return false;
673
+ throw new ConfigValidationError(path, value, 'boolean', 'Must be a boolean (true/false) or numeric (1/0)');
674
+ }
675
+ function validateNumber(value, path, options = {}) {
676
+ const num = Number(value);
677
+ if (isNaN(num)) {
678
+ throw new ConfigValidationError(path, value, 'number', 'Must be a valid number');
679
+ }
680
+ if (options.min !== undefined && num < options.min) {
681
+ throw new ConfigValidationError(path, value, `number >= ${options.min}`, `Must be at least ${options.min}`);
682
+ }
683
+ if (options.max !== undefined && num > options.max) {
684
+ throw new ConfigValidationError(path, value, `number <= ${options.max}`, `Must be at most ${options.max}`);
685
+ }
686
+ return num;
687
+ }
688
+ function validateString(value, path) {
689
+ if (typeof value !== 'string') {
690
+ throw new ConfigValidationError(path, value, 'string', 'Must be a string');
691
+ }
692
+ return value;
693
+ }
694
+ function validateEnum(value, validValues, path) {
695
+ const str = validateString(value, path);
696
+ if (!validValues.includes(str)) {
697
+ throw new ConfigValidationError(path, value, `one of: ${validValues.join(', ')}`, `Must be one of: ${validValues.join(', ')}`);
698
+ }
699
+ return str;
700
+ }
701
+ function validateStringArray(value, path) {
702
+ if (!Array.isArray(value)) {
703
+ // Try to parse comma-separated string
704
+ if (typeof value === 'string') {
705
+ return value
706
+ .split(',')
707
+ .map(s => s.trim())
708
+ .filter(s => s.length > 0);
709
+ }
710
+ throw new ConfigValidationError(path, value, 'string[]', 'Must be an array or comma-separated string');
711
+ }
712
+ return value.map((item, index) => validateString(item, `${path}[${index}]`));
713
+ }
714
+ function validateObject(value, path) {
715
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
716
+ throw new ConfigValidationError(path, value, 'object', 'Must be an object');
717
+ }
718
+ return value;
719
+ }
720
+ /**
721
+ * Provide helpful validation hints based on the error
722
+ */
723
+ function provideValidationHints(error) {
724
+ if (error.field.includes('port')) {
725
+ logger.error(' 💡 Hint: Ports must be numbers between 1 and 65535');
726
+ }
727
+ if (error.field.includes('url')) {
728
+ logger.error(' 💡 Hint: URLs must include protocol (http:// or https://)');
729
+ }
730
+ if (error.field.includes('environment')) {
731
+ logger.error(' 💡 Hint: NODE_ENV must be one of: development, staging, production');
732
+ }
733
+ if (error.field.includes('level')) {
734
+ logger.error(' 💡 Hint: Log level must be one of: debug, info, warn, error, fatal');
735
+ }
736
+ }
737
+ //# sourceMappingURL=config-validator.js.map