core-services-sdk 1.3.77 → 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
|
@@ -409,3 +409,82 @@ export function prepareEnvValidation(definition, values, options = {}) {
|
|
|
409
409
|
report,
|
|
410
410
|
}
|
|
411
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
|
+
}
|
|
@@ -2,6 +2,8 @@ import { describe, it, expect } from 'vitest'
|
|
|
2
2
|
import { prepareEnvValidation } from '../../src/env/env-validation.js'
|
|
3
3
|
|
|
4
4
|
describe('prepareEnvValidation', () => {
|
|
5
|
+
const mask = () => '****'
|
|
6
|
+
|
|
5
7
|
const definition = {
|
|
6
8
|
PORT: {
|
|
7
9
|
type: 'number',
|
|
@@ -31,13 +33,8 @@ describe('prepareEnvValidation', () => {
|
|
|
31
33
|
const result = prepareEnvValidation(definition, values, { mask })
|
|
32
34
|
|
|
33
35
|
expect(result.success).toBe(true)
|
|
34
|
-
expect(result.validation.success).toBe(true)
|
|
35
|
-
|
|
36
36
|
expect(result.table).toContain('Environment variables')
|
|
37
37
|
expect(result.table).toContain('PORT')
|
|
38
|
-
expect(result.table).toContain('MODE')
|
|
39
|
-
expect(result.table).toContain('API_KEY')
|
|
40
|
-
|
|
41
38
|
expect(result.table).toContain('OK')
|
|
42
39
|
expect(result.table).toContain('****')
|
|
43
40
|
expect(result.table).not.toContain('secret')
|
|
@@ -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
|
+
})
|
|
@@ -279,3 +279,57 @@ export function prepareEnvValidation(
|
|
|
279
279
|
}>
|
|
280
280
|
}
|
|
281
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
|
+
}
|