core-services-sdk 1.3.76 → 1.3.78

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.3.76",
3
+ "version": "1.3.78",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "types": "types/index.d.ts",
@@ -361,3 +361,130 @@ export function validateAndReportEnv(definition, values, options = {}) {
361
361
  output,
362
362
  }
363
363
  }
364
+
365
+ /**
366
+ * Validates environment variables and prepares
367
+ * a printable table output.
368
+ *
369
+ * No side effects. Does NOT print.
370
+ *
371
+ * @param {Record<string, Object>} definition
372
+ * @param {Record<string, any>} values
373
+ * @param {{
374
+ * mask?: (value: any) => string
375
+ * }} [options]
376
+ *
377
+ * @returns {{
378
+ * success: boolean,
379
+ * table: string,
380
+ * validation: {
381
+ * success: boolean,
382
+ * data?: Record<string, any>,
383
+ * summary?: Record<string, string[]>
384
+ * },
385
+ * report: {
386
+ * success: boolean,
387
+ * params: Array<{
388
+ * key: string,
389
+ * value: any,
390
+ * displayValue: string,
391
+ * secret: boolean,
392
+ * valid: boolean,
393
+ * errors?: string[]
394
+ * }>
395
+ * }
396
+ * }}
397
+ */
398
+ export function prepareEnvValidation(definition, values, options = {}) {
399
+ const { mask } = options
400
+
401
+ const validation = validateEnv(definition, values)
402
+ const report = buildEnvReport(definition, values, validation, mask)
403
+ const table = formatEnvReport(report)
404
+
405
+ return {
406
+ success: validation.success,
407
+ table,
408
+ validation,
409
+ report,
410
+ }
411
+ }
412
+
413
+ /**
414
+ * Builds a console-ready table output for environment validation.
415
+ *
416
+ * Always includes all variables.
417
+ * NOTES column always has a value:
418
+ * - 'OK' for valid variables
419
+ * - error message(s) for invalid variables
420
+ *
421
+ * @param {{
422
+ * params: Array<{
423
+ * key: string,
424
+ * displayValue: string,
425
+ * valid: boolean,
426
+ * errors?: string[]
427
+ * }>
428
+ * }} report
429
+ *
430
+ * @returns {string}
431
+ */
432
+ export function buildConsoleOutput(report) {
433
+ const headers = ['STATUS', 'KEY', 'VALUE', 'NOTES']
434
+
435
+ const rows = report.params.map((p) => {
436
+ const status = p.valid ? 'OK' : 'ERROR'
437
+ const notes = p.valid ? 'OK' : p.errors?.join('; ') || 'Invalid value'
438
+
439
+ return [status, p.key, p.displayValue, notes]
440
+ })
441
+
442
+ const allRows = [headers, ...rows]
443
+
444
+ const colWidths = headers.map((_, i) =>
445
+ Math.max(...allRows.map((row) => String(row[i]).length)),
446
+ )
447
+
448
+ const formatRow = (row) =>
449
+ row.map((cell, i) => String(cell).padEnd(colWidths[i])).join(' ')
450
+
451
+ return [
452
+ 'Environment variables',
453
+ '',
454
+ ...allRows.map(formatRow),
455
+ '',
456
+ report.params.every((p) => p.valid)
457
+ ? 'All variables are valid.'
458
+ : 'Some variables are invalid.',
459
+ ].join('\n')
460
+ }
461
+
462
+ /**
463
+ * Validates environment variables and prepares
464
+ * a fully printable console output.
465
+ *
466
+ * @param {Record<string, Object>} definition
467
+ * @param {Record<string, any>} values
468
+ * @param {{
469
+ * mask?: (value: any) => string
470
+ * }} [options]
471
+ *
472
+ * @returns {{
473
+ * success: boolean,
474
+ * output: string,
475
+ * report: any
476
+ * }}
477
+ */
478
+ export function validateEnvForConsole(definition, values, options = {}) {
479
+ const { mask } = options
480
+
481
+ const validation = validateEnv(definition, values)
482
+ const report = buildEnvReport(definition, values, validation, mask)
483
+ const output = buildConsoleOutput(report)
484
+
485
+ return {
486
+ output,
487
+ report,
488
+ success: validation.success,
489
+ }
490
+ }
@@ -0,0 +1,86 @@
1
+ // @ts-nocheck
2
+ import { prepareEnvValidation } from '../../src/env/env-validation.js'
3
+
4
+ const mask = (value) => {
5
+ if (value == null) {
6
+ return '******'
7
+ }
8
+
9
+ const str = String(value)
10
+ if (str.length <= 4) {
11
+ return '****'
12
+ }
13
+
14
+ return `${str.slice(0, 2)}****${str.slice(-2)}`
15
+ }
16
+
17
+ const definition = {
18
+ PORT: {
19
+ type: 'number',
20
+ required: true,
21
+ int: true,
22
+ min: 1,
23
+ max: 65535,
24
+ },
25
+ TIMEOUT: {
26
+ type: 'number',
27
+ min: 100,
28
+ max: 10000,
29
+ },
30
+ DEBUG: {
31
+ type: 'boolean',
32
+ },
33
+ MODE: {
34
+ type: 'string',
35
+ enum: ['development', 'production'],
36
+ },
37
+ API_KEY: {
38
+ type: 'string',
39
+ secret: true,
40
+ },
41
+ }
42
+
43
+ const scenarios = [
44
+ {
45
+ title: '✅ All valid values',
46
+ values: {
47
+ PORT: 3000,
48
+ TIMEOUT: 500,
49
+ DEBUG: false,
50
+ MODE: 'development',
51
+ API_KEY: 'secret123',
52
+ },
53
+ },
54
+ {
55
+ title: '❌ Invalid port',
56
+ values: {
57
+ PORT: 0,
58
+ TIMEOUT: 500,
59
+ MODE: 'production',
60
+ },
61
+ },
62
+ {
63
+ title: '❌ Invalid enum',
64
+ values: {
65
+ PORT: 3000,
66
+ TIMEOUT: 500,
67
+ MODE: 'prod',
68
+ API_KEY: 'secret123',
69
+ },
70
+ },
71
+ ]
72
+
73
+ for (const scenario of scenarios) {
74
+ console.log('\n===================================================')
75
+ console.log(scenario.title)
76
+ console.log('===================================================\n')
77
+
78
+ const result = prepareEnvValidation(definition, scenario.values, { mask })
79
+
80
+ // פה הבחירה שלך מה לעשות עם זה
81
+ console.log(result.table)
82
+
83
+ if (!result.success) {
84
+ console.log('⚠️ Scenario failed validation')
85
+ }
86
+ }
@@ -0,0 +1,81 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { prepareEnvValidation } from '../../src/env/env-validation.js'
3
+
4
+ describe('prepareEnvValidation', () => {
5
+ const mask = () => '****'
6
+
7
+ const definition = {
8
+ PORT: {
9
+ type: 'number',
10
+ required: true,
11
+ int: true,
12
+ min: 1,
13
+ max: 65535,
14
+ },
15
+ MODE: {
16
+ type: 'string',
17
+ enum: ['development', 'production'],
18
+ required: true,
19
+ },
20
+ API_KEY: {
21
+ type: 'string',
22
+ secret: true,
23
+ },
24
+ }
25
+
26
+ it('returns success and a printable table when values are valid', () => {
27
+ const values = {
28
+ PORT: '3000',
29
+ MODE: 'development',
30
+ API_KEY: 'secret',
31
+ }
32
+
33
+ const result = prepareEnvValidation(definition, values, { mask })
34
+
35
+ expect(result.success).toBe(true)
36
+ expect(result.table).toContain('Environment variables')
37
+ expect(result.table).toContain('PORT')
38
+ expect(result.table).toContain('OK')
39
+ expect(result.table).toContain('****')
40
+ expect(result.table).not.toContain('secret')
41
+ })
42
+
43
+ it('returns failure and error table when required value is missing', () => {
44
+ const values = {
45
+ MODE: 'production',
46
+ }
47
+
48
+ const result = prepareEnvValidation(definition, values)
49
+
50
+ expect(result.success).toBe(false)
51
+ expect(result.validation.success).toBe(false)
52
+
53
+ expect(result.table).toContain('ERROR')
54
+ expect(result.table).toContain('PORT')
55
+ })
56
+
57
+ it('coerces number values correctly', () => {
58
+ const values = {
59
+ PORT: '8080',
60
+ MODE: 'production',
61
+ API_KEY: 'x',
62
+ }
63
+
64
+ const result = prepareEnvValidation(definition, values)
65
+
66
+ expect(result.success).toBe(true)
67
+ expect(result.validation.data.PORT).toBe(8080)
68
+ })
69
+
70
+ it('returns a table string even on failure', () => {
71
+ const values = {
72
+ PORT: 0,
73
+ MODE: 'development',
74
+ }
75
+
76
+ const result = prepareEnvValidation(definition, values)
77
+
78
+ expect(typeof result.table).toBe('string')
79
+ expect(result.table.length).toBeGreaterThan(0)
80
+ })
81
+ })
@@ -0,0 +1,122 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { validateEnvForConsole } from '../../src/env/env-validation.js'
3
+
4
+ describe('validateEnvForConsole', () => {
5
+ const definition = {
6
+ PORT: {
7
+ type: 'number',
8
+ required: true,
9
+ int: true,
10
+ min: 1,
11
+ max: 65535,
12
+ },
13
+ MODE: {
14
+ type: 'string',
15
+ enum: ['development', 'production'],
16
+ required: true,
17
+ },
18
+ API_KEY: {
19
+ type: 'string',
20
+ secret: true,
21
+ },
22
+ }
23
+
24
+ const mask = () => '****'
25
+
26
+ it('returns a printable table with all variables and OK notes when valid', () => {
27
+ const values = {
28
+ PORT: '3000',
29
+ MODE: 'development',
30
+ API_KEY: 'secret',
31
+ }
32
+
33
+ const result = validateEnvForConsole(definition, values, { mask })
34
+
35
+ expect(result.success).toBe(true)
36
+ expect(typeof result.output).toBe('string')
37
+
38
+ // header
39
+ expect(result.output).toContain('Environment variables')
40
+ expect(result.output).toContain('STATUS')
41
+ expect(result.output).toContain('KEY')
42
+ expect(result.output).toContain('VALUE')
43
+ expect(result.output).toContain('NOTES')
44
+
45
+ // rows
46
+ expect(result.output).toContain('PORT')
47
+ expect(result.output).toContain('MODE')
48
+ expect(result.output).toContain('API_KEY')
49
+
50
+ // notes always present
51
+ expect(result.output).toContain('OK PORT')
52
+ expect(result.output).toContain('OK MODE')
53
+ expect(result.output).toContain('OK API_KEY')
54
+
55
+ // masking
56
+ expect(result.output).toContain('****')
57
+ expect(result.output).not.toContain('secret')
58
+ })
59
+
60
+ it('returns a printable table with ERROR notes when invalid', () => {
61
+ const values = {
62
+ PORT: '0',
63
+ MODE: 'prod',
64
+ API_KEY: 'secret',
65
+ }
66
+
67
+ const result = validateEnvForConsole(definition, values, { mask })
68
+
69
+ expect(result.success).toBe(false)
70
+ expect(typeof result.output).toBe('string')
71
+
72
+ // all variables still appear
73
+ expect(result.output).toContain('PORT')
74
+ expect(result.output).toContain('MODE')
75
+ expect(result.output).toContain('API_KEY')
76
+
77
+ // error rows
78
+ expect(result.output).toContain('ERROR PORT')
79
+ expect(result.output).toContain('ERROR MODE')
80
+
81
+ // notes are NOT empty
82
+ expect(result.output).toMatch(/PORT\s+0\s+.+/)
83
+ expect(result.output).toMatch(/MODE\s+prod\s+.+/)
84
+
85
+ // summary line
86
+ expect(result.output).toContain('Some variables are invalid.')
87
+ })
88
+
89
+ it('always sets NOTES to OK for valid variables even if others fail', () => {
90
+ const values = {
91
+ PORT: '3000',
92
+ MODE: 'prod', // invalid
93
+ API_KEY: 'secret',
94
+ }
95
+
96
+ const result = validateEnvForConsole(definition, values, { mask })
97
+
98
+ // PORT and API_KEY are valid → NOTES = OK
99
+ expect(result.output).toMatch(/OK\s+PORT\s+3000\s+OK/)
100
+ expect(result.output).toMatch(/OK\s+API_KEY\s+\*\*\*\*\s+OK/)
101
+
102
+ // MODE is invalid → NOTES = error message
103
+ expect(result.output).toMatch(/ERROR\s+MODE\s+prod\s+.+/)
104
+ })
105
+
106
+ it('always returns output even if multiple errors exist', () => {
107
+ const values = {
108
+ MODE: 'prod',
109
+ }
110
+
111
+ const result = validateEnvForConsole(definition, values)
112
+
113
+ expect(result.success).toBe(false)
114
+ expect(typeof result.output).toBe('string')
115
+ expect(result.output.length).toBeGreaterThan(0)
116
+
117
+ // all keys still listed
118
+ expect(result.output).toContain('PORT')
119
+ expect(result.output).toContain('MODE')
120
+ expect(result.output).toContain('API_KEY')
121
+ })
122
+ })
@@ -220,3 +220,116 @@ export function validateAndReportEnv(
220
220
  }
221
221
  output: string
222
222
  }
223
+ /**
224
+ * Validates environment variables and prepares
225
+ * a printable table output.
226
+ *
227
+ * No side effects. Does NOT print.
228
+ *
229
+ * @param {Record<string, Object>} definition
230
+ * @param {Record<string, any>} values
231
+ * @param {{
232
+ * mask?: (value: any) => string
233
+ * }} [options]
234
+ *
235
+ * @returns {{
236
+ * success: boolean,
237
+ * table: string,
238
+ * validation: {
239
+ * success: boolean,
240
+ * data?: Record<string, any>,
241
+ * summary?: Record<string, string[]>
242
+ * },
243
+ * report: {
244
+ * success: boolean,
245
+ * params: Array<{
246
+ * key: string,
247
+ * value: any,
248
+ * displayValue: string,
249
+ * secret: boolean,
250
+ * valid: boolean,
251
+ * errors?: string[]
252
+ * }>
253
+ * }
254
+ * }}
255
+ */
256
+ export function prepareEnvValidation(
257
+ definition: Record<string, any>,
258
+ values: Record<string, any>,
259
+ options?: {
260
+ mask?: (value: any) => string
261
+ },
262
+ ): {
263
+ success: boolean
264
+ table: string
265
+ validation: {
266
+ success: boolean
267
+ data?: Record<string, any>
268
+ summary?: Record<string, string[]>
269
+ }
270
+ report: {
271
+ success: boolean
272
+ params: Array<{
273
+ key: string
274
+ value: any
275
+ displayValue: string
276
+ secret: boolean
277
+ valid: boolean
278
+ errors?: string[]
279
+ }>
280
+ }
281
+ }
282
+ /**
283
+ * Builds a console-ready table output for environment validation.
284
+ *
285
+ * Always includes all variables.
286
+ * NOTES column always has a value:
287
+ * - 'OK' for valid variables
288
+ * - error message(s) for invalid variables
289
+ *
290
+ * @param {{
291
+ * params: Array<{
292
+ * key: string,
293
+ * displayValue: string,
294
+ * valid: boolean,
295
+ * errors?: string[]
296
+ * }>
297
+ * }} report
298
+ *
299
+ * @returns {string}
300
+ */
301
+ export function buildConsoleOutput(report: {
302
+ params: Array<{
303
+ key: string
304
+ displayValue: string
305
+ valid: boolean
306
+ errors?: string[]
307
+ }>
308
+ }): string
309
+ /**
310
+ * Validates environment variables and prepares
311
+ * a fully printable console output.
312
+ *
313
+ * @param {Record<string, Object>} definition
314
+ * @param {Record<string, any>} values
315
+ * @param {{
316
+ * mask?: (value: any) => string
317
+ * }} [options]
318
+ *
319
+ * @returns {{
320
+ * success: boolean,
321
+ * output: string,
322
+ * report: any
323
+ * }}
324
+ */
325
+ export function validateEnvForConsole(
326
+ definition: Record<string, any>,
327
+ values: Record<string, any>,
328
+ options?: {
329
+ mask?: (value: any) => string
330
+ },
331
+ ): {
332
+ success: boolean
333
+ output: string
334
+ report: any
335
+ }