core-services-sdk 1.3.76 → 1.3.77
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
|
@@ -361,3 +361,51 @@ 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
|
+
}
|
|
@@ -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,84 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { prepareEnvValidation } from '../../src/env/env-validation.js'
|
|
3
|
+
|
|
4
|
+
describe('prepareEnvValidation', () => {
|
|
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
|
+
it('returns success and a printable table when values are valid', () => {
|
|
25
|
+
const values = {
|
|
26
|
+
PORT: '3000',
|
|
27
|
+
MODE: 'development',
|
|
28
|
+
API_KEY: 'secret',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const result = prepareEnvValidation(definition, values, { mask })
|
|
32
|
+
|
|
33
|
+
expect(result.success).toBe(true)
|
|
34
|
+
expect(result.validation.success).toBe(true)
|
|
35
|
+
|
|
36
|
+
expect(result.table).toContain('Environment variables')
|
|
37
|
+
expect(result.table).toContain('PORT')
|
|
38
|
+
expect(result.table).toContain('MODE')
|
|
39
|
+
expect(result.table).toContain('API_KEY')
|
|
40
|
+
|
|
41
|
+
expect(result.table).toContain('OK')
|
|
42
|
+
expect(result.table).toContain('****')
|
|
43
|
+
expect(result.table).not.toContain('secret')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('returns failure and error table when required value is missing', () => {
|
|
47
|
+
const values = {
|
|
48
|
+
MODE: 'production',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const result = prepareEnvValidation(definition, values)
|
|
52
|
+
|
|
53
|
+
expect(result.success).toBe(false)
|
|
54
|
+
expect(result.validation.success).toBe(false)
|
|
55
|
+
|
|
56
|
+
expect(result.table).toContain('ERROR')
|
|
57
|
+
expect(result.table).toContain('PORT')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('coerces number values correctly', () => {
|
|
61
|
+
const values = {
|
|
62
|
+
PORT: '8080',
|
|
63
|
+
MODE: 'production',
|
|
64
|
+
API_KEY: 'x',
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const result = prepareEnvValidation(definition, values)
|
|
68
|
+
|
|
69
|
+
expect(result.success).toBe(true)
|
|
70
|
+
expect(result.validation.data.PORT).toBe(8080)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('returns a table string even on failure', () => {
|
|
74
|
+
const values = {
|
|
75
|
+
PORT: 0,
|
|
76
|
+
MODE: 'development',
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const result = prepareEnvValidation(definition, values)
|
|
80
|
+
|
|
81
|
+
expect(typeof result.table).toBe('string')
|
|
82
|
+
expect(result.table.length).toBeGreaterThan(0)
|
|
83
|
+
})
|
|
84
|
+
})
|
|
@@ -220,3 +220,62 @@ 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
|
+
}
|