core-services-sdk 1.3.77 → 1.3.80
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 +1 -1
- package/src/env/env-validation.js +80 -1
- package/src/util/mask-sensitive.js +9 -3
- package/tests/env/prepare-env-validation.test.js +3 -5
- package/tests/env/validate-env-for-console.test.js +122 -0
- package/tests/util/mask-sensitive.unit.test.js +29 -8
- package/types/env/env-validation.d.ts +54 -0
package/package.json
CHANGED
|
@@ -240,7 +240,7 @@ export function buildEnvReport(definition, values, validationResult, mask) {
|
|
|
240
240
|
? validationResult.data[key]
|
|
241
241
|
: rawValue
|
|
242
242
|
|
|
243
|
-
const displayValue = isSecret ? maskValue(value) : String(value)
|
|
243
|
+
const displayValue = isSecret ? maskValue(value, '.', 3) : String(value)
|
|
244
244
|
|
|
245
245
|
const errors = validationResult.success
|
|
246
246
|
? undefined
|
|
@@ -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
|
+
}
|
|
@@ -17,7 +17,9 @@ export const maskSingle = (
|
|
|
17
17
|
if (value == null) {
|
|
18
18
|
return ''
|
|
19
19
|
}
|
|
20
|
+
|
|
20
21
|
const str = String(value)
|
|
22
|
+
|
|
21
23
|
if (str.length === 0) {
|
|
22
24
|
return ''
|
|
23
25
|
}
|
|
@@ -25,16 +27,20 @@ export const maskSingle = (
|
|
|
25
27
|
if (typeof value === 'boolean') {
|
|
26
28
|
return str
|
|
27
29
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
|
|
31
|
+
// default: max 3 mask characters
|
|
32
|
+
const calculated = Math.max(1, str.length - (left + right))
|
|
33
|
+
const m = maskLen == null ? Math.min(3, calculated) : Math.max(0, maskLen)
|
|
30
34
|
|
|
31
35
|
if (str.length <= left + right) {
|
|
32
36
|
if (str.length === 1) {
|
|
33
37
|
return fill
|
|
34
38
|
}
|
|
39
|
+
|
|
35
40
|
if (str.length === 2) {
|
|
36
|
-
return str[0] + fill
|
|
41
|
+
return str[0] + fill
|
|
37
42
|
}
|
|
43
|
+
|
|
38
44
|
return str.slice(0, 1) + fill.repeat(m) + str.slice(-1)
|
|
39
45
|
}
|
|
40
46
|
|
|
@@ -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')
|
|
@@ -67,6 +64,7 @@ describe('prepareEnvValidation', () => {
|
|
|
67
64
|
const result = prepareEnvValidation(definition, values)
|
|
68
65
|
|
|
69
66
|
expect(result.success).toBe(true)
|
|
67
|
+
// @ts-ignore
|
|
70
68
|
expect(result.validation.data.PORT).toBe(8080)
|
|
71
69
|
})
|
|
72
70
|
|
|
@@ -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
|
+
})
|
|
@@ -5,11 +5,11 @@ import { mask, maskSingle } from '../../src/util/index.js'
|
|
|
5
5
|
describe('maskSingle', () => {
|
|
6
6
|
it('masks middle of a regular string (length == left+right)', () => {
|
|
7
7
|
const value = maskSingle('abcdefgh')
|
|
8
|
-
expect(value).toBe('ab
|
|
8
|
+
expect(value).toBe('ab...gh')
|
|
9
9
|
})
|
|
10
10
|
|
|
11
11
|
it('masks numbers with default settings', () => {
|
|
12
|
-
expect(maskSingle(12345678)).toBe('12
|
|
12
|
+
expect(maskSingle(12345678)).toBe('12...78')
|
|
13
13
|
})
|
|
14
14
|
|
|
15
15
|
it('masks booleans', () => {
|
|
@@ -45,8 +45,8 @@ describe('maskSingle', () => {
|
|
|
45
45
|
|
|
46
46
|
describe('mask', () => {
|
|
47
47
|
it('masks primitives (string, number, boolean)', () => {
|
|
48
|
-
expect(mask('abcdefgh')).toBe('ab
|
|
49
|
-
expect(mask(12345678)).toBe('12
|
|
48
|
+
expect(mask('abcdefgh')).toBe('ab...gh')
|
|
49
|
+
expect(mask(12345678)).toBe('12...78')
|
|
50
50
|
const trueValue = mask(true)
|
|
51
51
|
expect(trueValue).toBe('true')
|
|
52
52
|
expect(mask(false)).toBe('false')
|
|
@@ -59,20 +59,20 @@ describe('mask', () => {
|
|
|
59
59
|
|
|
60
60
|
it('masks arrays recursively', () => {
|
|
61
61
|
const value = mask(['abcdefgh', 12345678])
|
|
62
|
-
expect(value).toEqual(['ab
|
|
62
|
+
expect(value).toEqual(['ab...gh', '12...78'])
|
|
63
63
|
})
|
|
64
64
|
|
|
65
65
|
it('masks objects recursively', () => {
|
|
66
66
|
expect(mask({ a: 'abcdefgh', b: 12345678 })).toEqual({
|
|
67
|
-
a: 'ab
|
|
68
|
-
b: '12
|
|
67
|
+
a: 'ab...gh',
|
|
68
|
+
b: '12...78',
|
|
69
69
|
})
|
|
70
70
|
})
|
|
71
71
|
|
|
72
72
|
it('masks nested objects/arrays recursively', () => {
|
|
73
73
|
const input = { arr: ['abcdefgh', { num: 12345678 }] }
|
|
74
74
|
const expected = { arr: ['ab....gh', { num: '12....78' }] }
|
|
75
|
-
const value = mask(input)
|
|
75
|
+
const value = mask(input, undefined, 4)
|
|
76
76
|
expect(value).toEqual(expected)
|
|
77
77
|
})
|
|
78
78
|
|
|
@@ -96,4 +96,25 @@ describe('mask', () => {
|
|
|
96
96
|
const value = mask(input, '*', 3)
|
|
97
97
|
expect(value).toEqual(expected)
|
|
98
98
|
})
|
|
99
|
+
|
|
100
|
+
it('limits default mask to max 3 characters', () => {
|
|
101
|
+
const value = maskSingle('abcdefghijklmnop')
|
|
102
|
+
expect(value).toBe('ab...op')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('does not exceed available middle length', () => {
|
|
106
|
+
const value = maskSingle('abcde') // left=2 right=2 → middle=1
|
|
107
|
+
expect(value).toBe('ab.de')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('allows maskLen = 0', () => {
|
|
111
|
+
const value = maskSingle('abcdefgh', '.', 0)
|
|
112
|
+
expect(value).toBe('abgh')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('handles Date inside object', () => {
|
|
116
|
+
const d = new Date('2025-01-01T00:00:00.000Z')
|
|
117
|
+
const value = mask({ date: d })
|
|
118
|
+
expect(value).toEqual({ date: d.toISOString() })
|
|
119
|
+
})
|
|
99
120
|
})
|
|
@@ -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
|
+
}
|