@achs/env 3.1.0 → 3.1.1
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/arguments.d.ts +25 -0
- package/arguments.d.ts.map +1 -0
- package/{src/arguments.ts → arguments.js} +8 -32
- package/arguments.js.map +1 -0
- package/commands/env.command.d.ts +8 -0
- package/commands/env.command.d.ts.map +1 -0
- package/commands/env.command.js +85 -0
- package/commands/env.command.js.map +1 -0
- package/commands/export.command.d.ts +8 -0
- package/commands/export.command.d.ts.map +1 -0
- package/commands/export.command.js +54 -0
- package/commands/export.command.js.map +1 -0
- package/{src/commands/index.ts → commands/index.d.ts} +1 -0
- package/commands/index.d.ts.map +1 -0
- package/commands/index.js +14 -0
- package/commands/index.js.map +1 -0
- package/commands/pull.command.d.ts +7 -0
- package/commands/pull.command.d.ts.map +1 -0
- package/commands/pull.command.js +39 -0
- package/commands/pull.command.js.map +1 -0
- package/commands/push.command.d.ts +7 -0
- package/commands/push.command.d.ts.map +1 -0
- package/commands/push.command.js +38 -0
- package/commands/push.command.js.map +1 -0
- package/commands/schema.command.d.ts +4 -0
- package/commands/schema.command.d.ts.map +1 -0
- package/commands/schema.command.js +18 -0
- package/commands/schema.command.js.map +1 -0
- package/exec.d.ts +3 -0
- package/exec.d.ts.map +1 -0
- package/exec.js +142 -0
- package/exec.js.map +1 -0
- package/{src/index.ts → index.d.ts} +1 -0
- package/index.d.ts.map +1 -0
- package/index.js +20 -0
- package/index.js.map +1 -0
- package/{src/interfaces/index.ts → interfaces/index.d.ts} +1 -0
- package/interfaces/index.d.ts.map +1 -0
- package/interfaces/index.js +18 -0
- package/interfaces/index.js.map +1 -0
- package/interfaces/loader.interface.d.ts +21 -0
- package/interfaces/loader.interface.d.ts.map +1 -0
- package/interfaces/loader.interface.js +3 -0
- package/interfaces/loader.interface.js.map +1 -0
- package/main.d.ts +3 -0
- package/main.d.ts.map +1 -0
- package/main.js +6 -0
- package/main.js.map +1 -0
- package/package.json +1 -1
- package/providers/app-settings.provider.d.ts +8 -0
- package/providers/app-settings.provider.d.ts.map +1 -0
- package/providers/app-settings.provider.js +50 -0
- package/providers/app-settings.provider.js.map +1 -0
- package/providers/azure-key-vault.provider.d.ts +20 -0
- package/providers/azure-key-vault.provider.d.ts.map +1 -0
- package/providers/azure-key-vault.provider.js +143 -0
- package/providers/azure-key-vault.provider.js.map +1 -0
- package/providers/index.d.ts +7 -0
- package/providers/index.d.ts.map +1 -0
- package/providers/index.js +30 -0
- package/providers/index.js.map +1 -0
- package/providers/local.provider.d.ts +8 -0
- package/providers/local.provider.d.ts.map +1 -0
- package/providers/local.provider.js +31 -0
- package/providers/local.provider.js.map +1 -0
- package/providers/package-json.provider.d.ts +8 -0
- package/providers/package-json.provider.d.ts.map +1 -0
- package/providers/package-json.provider.js +29 -0
- package/providers/package-json.provider.js.map +1 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/utils/command.util.d.ts +13 -0
- package/utils/command.util.d.ts.map +1 -0
- package/utils/command.util.js +134 -0
- package/utils/command.util.js.map +1 -0
- package/{src/utils/index.ts → utils/index.d.ts} +1 -0
- package/utils/index.d.ts.map +1 -0
- package/utils/index.js +23 -0
- package/utils/index.js.map +1 -0
- package/utils/interpolate.util.d.ts +4 -0
- package/utils/interpolate.util.d.ts.map +1 -0
- package/utils/interpolate.util.js +33 -0
- package/utils/interpolate.util.js.map +1 -0
- package/utils/json.util.d.ts +5 -0
- package/utils/json.util.d.ts.map +1 -0
- package/utils/json.util.js +48 -0
- package/utils/json.util.js.map +1 -0
- package/utils/logger.d.ts +3 -0
- package/utils/logger.d.ts.map +1 -0
- package/{src/utils/logger.ts → utils/logger.js} +6 -6
- package/utils/logger.js.map +1 -0
- package/utils/normalize.util.d.ts +3 -0
- package/utils/normalize.util.d.ts.map +1 -0
- package/utils/normalize.util.js +61 -0
- package/utils/normalize.util.js.map +1 -0
- package/utils/schema.util.d.ts +11 -0
- package/utils/schema.util.d.ts.map +1 -0
- package/utils/schema.util.js +100 -0
- package/utils/schema.util.js.map +1 -0
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -329
- package/.vscode/extensions.json +0 -18
- package/.vscode/launch.json +0 -30
- package/.vscode/settings.json +0 -29
- package/jest.config.json +0 -28
- package/src/commands/env.command.ts +0 -139
- package/src/commands/export.command.ts +0 -88
- package/src/commands/pull.command.ts +0 -52
- package/src/commands/push.command.ts +0 -48
- package/src/commands/schema.command.ts +0 -31
- package/src/exec.ts +0 -221
- package/src/interfaces/loader.interface.ts +0 -66
- package/src/main.ts +0 -6
- package/src/providers/app-settings.provider.ts +0 -67
- package/src/providers/azure-key-vault.provider.ts +0 -277
- package/src/providers/index.ts +0 -29
- package/src/providers/local.provider.ts +0 -44
- package/src/providers/package-json.provider.ts +0 -39
- package/src/utils/command.util.ts +0 -223
- package/src/utils/interpolate.util.ts +0 -65
- package/src/utils/json.util.ts +0 -116
- package/src/utils/normalize.util.ts +0 -142
- package/src/utils/schema.util.ts +0 -191
- package/tests/env/appsettings.json +0 -32
- package/tests/env/dev.env.json +0 -12
- package/tests/env/dev.local.env.json +0 -9
- package/tests/env/env.schema.json +0 -225
- package/tests/env/keys.json +0 -7
- package/tests/env/settings/schema.json +0 -239
- package/tests/env/settings/settings.json +0 -22
- package/tests/env.int.test.ts +0 -42
- package/tests/exec.ts +0 -19
- package/tests/export.int.test.ts +0 -9
- package/tests/pull-push.int.test.ts +0 -15
- package/tests/run.js +0 -32
- package/tests/schema.int.test.ts +0 -9
- package/tests/setup.ts +0 -13
- package/tsconfig.build.json +0 -10
- package/tsconfig.json +0 -37
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import subslate from 'subslate';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Validates value must be a record.
|
|
5
|
-
*
|
|
6
|
-
* @export
|
|
7
|
-
* @param {unknown} obj
|
|
8
|
-
* @returns {*} {obj is Record<string, unknown>}
|
|
9
|
-
*/
|
|
10
|
-
export function isRecord(obj: unknown): obj is Record<string, unknown> {
|
|
11
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
12
|
-
|
|
13
|
-
return Object.keys(obj).length > 0;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Replaces string arguments with regex interpolation.
|
|
18
|
-
*
|
|
19
|
-
* @export
|
|
20
|
-
* @template T
|
|
21
|
-
* @param {T} value
|
|
22
|
-
* @param {Record<string, unknown>} args
|
|
23
|
-
* @param {[string, string]} [delimiters=['[[', ']]']]
|
|
24
|
-
*
|
|
25
|
-
* @returns {T} mutated value
|
|
26
|
-
*/
|
|
27
|
-
export function interpolate<T extends string | unknown>(
|
|
28
|
-
value: T,
|
|
29
|
-
args: Record<string, unknown>,
|
|
30
|
-
delimiters: [string, string] = ['[[', ']]']
|
|
31
|
-
): T {
|
|
32
|
-
if (typeof value === 'string') {
|
|
33
|
-
return subslate(value, args, {
|
|
34
|
-
startStopPairs: delimiters
|
|
35
|
-
}) as T;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (Array.isArray(value))
|
|
39
|
-
return value.map((a) => interpolate(a, args, delimiters)) as T;
|
|
40
|
-
|
|
41
|
-
if (isRecord(value)) return interpolateJson(value, args, delimiters) as T;
|
|
42
|
-
|
|
43
|
-
return value;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Replaces JSON string arguments with regex interpolation.
|
|
48
|
-
*
|
|
49
|
-
* @export
|
|
50
|
-
* @param {Record<string, unknown>} args
|
|
51
|
-
* @param {Record<string, unknown>} values
|
|
52
|
-
* @param {[string, string]} [delimiters=['[[', ']]']]
|
|
53
|
-
*
|
|
54
|
-
* @returns {Record<string, unknown>} mutated args
|
|
55
|
-
*/
|
|
56
|
-
export function interpolateJson(
|
|
57
|
-
values: Record<string, unknown>,
|
|
58
|
-
args: Record<string, unknown>,
|
|
59
|
-
delimiters: [string, string] = ['[[', ']]']
|
|
60
|
-
): Record<string, unknown> {
|
|
61
|
-
for (const key in values)
|
|
62
|
-
values[key] = interpolate(values[key], args, delimiters);
|
|
63
|
-
|
|
64
|
-
return values;
|
|
65
|
-
}
|
package/src/utils/json.util.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import os from 'os';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { existsSync } from 'fs';
|
|
4
|
-
import { readFile, writeFile } from 'fs/promises';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Replaces undefined by null in JSON.stringify()
|
|
8
|
-
*
|
|
9
|
-
* @param {string} _ property key
|
|
10
|
-
* @param {any} value property value
|
|
11
|
-
*
|
|
12
|
-
* @returns {any} value
|
|
13
|
-
*/
|
|
14
|
-
const replacer = (_: string, value: any): typeof value | null =>
|
|
15
|
-
value === undefined ? null : value;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Resolve a relative path for os.
|
|
19
|
-
*
|
|
20
|
-
* @export
|
|
21
|
-
* @param {string} filePath relative path from project root
|
|
22
|
-
*
|
|
23
|
-
* @returns {string} path
|
|
24
|
-
*/
|
|
25
|
-
export function resolvePath(filePath: string): string {
|
|
26
|
-
const home = os.homedir();
|
|
27
|
-
|
|
28
|
-
if (home !== undefined)
|
|
29
|
-
filePath = filePath.replace(/^~($|\/|\\)/, `${home}$1`);
|
|
30
|
-
|
|
31
|
-
return path.resolve(process.cwd(), filePath);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Reads and parses a JSON file.
|
|
36
|
-
*
|
|
37
|
-
* @export
|
|
38
|
-
* @template T
|
|
39
|
-
* @param {string} path
|
|
40
|
-
*
|
|
41
|
-
* @returns {Promise<[Record<string, any>, boolean]>}
|
|
42
|
-
*/
|
|
43
|
-
export async function readJson<T = Record<string, any>>(
|
|
44
|
-
path: string
|
|
45
|
-
): Promise<[T | Record<string, any>, boolean] | never> {
|
|
46
|
-
if (!existsSync(path)) return [{}, false];
|
|
47
|
-
|
|
48
|
-
return [JSON.parse(await readFile(path, 'utf8')), true];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Saves a JSON into a file.
|
|
53
|
-
*
|
|
54
|
-
* @export
|
|
55
|
-
* @param {string} path
|
|
56
|
-
* @param {unknown} content
|
|
57
|
-
* @param {false} overwrite
|
|
58
|
-
* @param {false} undefinedAsNull replaces undefined by null
|
|
59
|
-
*
|
|
60
|
-
* @returns {Promise<boolean>}
|
|
61
|
-
*/
|
|
62
|
-
export async function writeJson(
|
|
63
|
-
path: string,
|
|
64
|
-
content: Record<string, unknown>,
|
|
65
|
-
overwrite = false,
|
|
66
|
-
undefinedAsNull = false
|
|
67
|
-
): Promise<boolean | never> {
|
|
68
|
-
const exists = existsSync(path);
|
|
69
|
-
|
|
70
|
-
if (exists && !overwrite) return false;
|
|
71
|
-
|
|
72
|
-
await writeFile(
|
|
73
|
-
path,
|
|
74
|
-
`${JSON.stringify(
|
|
75
|
-
content,
|
|
76
|
-
undefinedAsNull ? replacer : undefined,
|
|
77
|
-
4
|
|
78
|
-
)}\n`,
|
|
79
|
-
'utf8'
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Saves a JSON into a file as dotenv.
|
|
87
|
-
*
|
|
88
|
-
* @export
|
|
89
|
-
* @param {string} path
|
|
90
|
-
* @param {unknown} content
|
|
91
|
-
* @param {false} overwrite
|
|
92
|
-
*
|
|
93
|
-
* @returns {Promise<boolean>}
|
|
94
|
-
*/
|
|
95
|
-
export async function writeEnvFromJson(
|
|
96
|
-
path: string,
|
|
97
|
-
content: Record<string, unknown>,
|
|
98
|
-
overwrite = false
|
|
99
|
-
): Promise<boolean | never> {
|
|
100
|
-
const exists = existsSync(path);
|
|
101
|
-
|
|
102
|
-
if (exists && !overwrite) return false;
|
|
103
|
-
|
|
104
|
-
let data = '';
|
|
105
|
-
|
|
106
|
-
for (const key in content) {
|
|
107
|
-
let value = content[key];
|
|
108
|
-
if (typeof value === 'string') value = `"${value}"`;
|
|
109
|
-
|
|
110
|
-
data += `${key}=${value}\n`;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
await writeFile(path, data, 'utf8');
|
|
114
|
-
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Flatten a object keeping depth path
|
|
3
|
-
* in key using __ as level separator.
|
|
4
|
-
*
|
|
5
|
-
* @param {Record<string, any>} obj
|
|
6
|
-
* @param {string} nestingDelimiter char for delimit nesting levels
|
|
7
|
-
* @param {boolean} arrayDescomposition serialize or break down arrays
|
|
8
|
-
* @param {boolean} clearNull if null should removed from object
|
|
9
|
-
* @param {string} pkey first level key
|
|
10
|
-
*
|
|
11
|
-
* @returns {Record<string, string>} flattended object
|
|
12
|
-
*/
|
|
13
|
-
export function flatten(
|
|
14
|
-
obj: Record<string, any>,
|
|
15
|
-
nestingDelimiter = '__',
|
|
16
|
-
pkey = ''
|
|
17
|
-
): Record<string, string> {
|
|
18
|
-
const flattened: Record<string, string> = {};
|
|
19
|
-
|
|
20
|
-
for (let key in obj) {
|
|
21
|
-
const value = obj[key];
|
|
22
|
-
const type = typeof value;
|
|
23
|
-
|
|
24
|
-
if (value === undefined || type === 'function') continue;
|
|
25
|
-
|
|
26
|
-
// skipped property
|
|
27
|
-
if (key[0] === '#') continue;
|
|
28
|
-
key = pkey + key;
|
|
29
|
-
|
|
30
|
-
if (value === null || type !== 'object' || Array.isArray(value)) {
|
|
31
|
-
flattened[key] = value;
|
|
32
|
-
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
Object.assign(
|
|
37
|
-
flattened,
|
|
38
|
-
flatten(value, nestingDelimiter, `${key}${nestingDelimiter}`)
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return flattened;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Normalizes env object, converts arrays in list strings,
|
|
47
|
-
* only primitives types array,
|
|
48
|
-
* and removes $ global character from keys.
|
|
49
|
-
*
|
|
50
|
-
* @param {Record<string, any>} obj
|
|
51
|
-
* @param {string} nestingDelimiter char for delimit nesting levels
|
|
52
|
-
* @param {boolean} arrayDescomposition serialize or break down arrays
|
|
53
|
-
* @param {string} pkey first level key
|
|
54
|
-
*
|
|
55
|
-
* @returns {Record<string, string>} normalized object
|
|
56
|
-
*/
|
|
57
|
-
export function normalize(
|
|
58
|
-
obj: Record<string, any>,
|
|
59
|
-
nestingDelimiter = '__',
|
|
60
|
-
arrayDescomposition = false,
|
|
61
|
-
pkey = ''
|
|
62
|
-
): Record<string, string> {
|
|
63
|
-
const flattened: Record<string, string> = {};
|
|
64
|
-
|
|
65
|
-
for (let key in obj) {
|
|
66
|
-
const value = obj[key];
|
|
67
|
-
const type = typeof value;
|
|
68
|
-
|
|
69
|
-
if (value === null || value === undefined || type === 'function')
|
|
70
|
-
continue;
|
|
71
|
-
|
|
72
|
-
// global property, but prefix removed for injection
|
|
73
|
-
key = pkey + key.replace('$', '');
|
|
74
|
-
|
|
75
|
-
if (type !== 'object') {
|
|
76
|
-
flattened[key] = value;
|
|
77
|
-
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (Array.isArray(value)) {
|
|
82
|
-
normalizeArray(
|
|
83
|
-
flattened,
|
|
84
|
-
key,
|
|
85
|
-
value,
|
|
86
|
-
nestingDelimiter,
|
|
87
|
-
arrayDescomposition
|
|
88
|
-
);
|
|
89
|
-
} else {
|
|
90
|
-
Object.assign(
|
|
91
|
-
flattened,
|
|
92
|
-
normalize(
|
|
93
|
-
value,
|
|
94
|
-
nestingDelimiter,
|
|
95
|
-
arrayDescomposition,
|
|
96
|
-
`${key}${nestingDelimiter}`
|
|
97
|
-
)
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return flattened;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Flatten and normalizes an array.
|
|
107
|
-
*
|
|
108
|
-
* @param {Record<string, string>} flattened
|
|
109
|
-
* @param {string} key
|
|
110
|
-
* @param {any[]} value
|
|
111
|
-
* @param {string} [nestingDelimiter='__']
|
|
112
|
-
* @param {boolean} [arrayDescomposition=false]
|
|
113
|
-
*/
|
|
114
|
-
function normalizeArray(
|
|
115
|
-
flattened: Record<string, string>,
|
|
116
|
-
key: string,
|
|
117
|
-
value: any[],
|
|
118
|
-
nestingDelimiter = '__',
|
|
119
|
-
arrayDescomposition = false
|
|
120
|
-
): void {
|
|
121
|
-
if (arrayDescomposition) {
|
|
122
|
-
key = `${key}${nestingDelimiter}`;
|
|
123
|
-
|
|
124
|
-
for (let i = 0; i < value.length; i++) {
|
|
125
|
-
if (typeof value[i] === 'object') {
|
|
126
|
-
Object.assign(
|
|
127
|
-
flattened,
|
|
128
|
-
normalize(
|
|
129
|
-
value[i],
|
|
130
|
-
nestingDelimiter,
|
|
131
|
-
arrayDescomposition,
|
|
132
|
-
`${key}${i}${nestingDelimiter}`
|
|
133
|
-
)
|
|
134
|
-
);
|
|
135
|
-
} else {
|
|
136
|
-
flattened[`${key}${i}`] = value[i];
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
flattened[key] = value.filter((v) => typeof v !== 'object').join(',');
|
|
141
|
-
}
|
|
142
|
-
}
|
package/src/utils/schema.util.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import Ajv, { Format, JSONSchemaType, ValidateFunction } from 'ajv';
|
|
2
|
-
import addFormats from 'ajv-formats';
|
|
3
|
-
import toJsonSchema, { Options } from 'to-json-schema';
|
|
4
|
-
|
|
5
|
-
const FORMAT_REGEXPS: Record<string, Format> = {
|
|
6
|
-
'ip-address':
|
|
7
|
-
/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})$/,
|
|
8
|
-
|
|
9
|
-
color: /^(#?([\dA-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b\s*,\s*\b(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b\s*,\s*\b(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,
|
|
10
|
-
|
|
11
|
-
hostname:
|
|
12
|
-
/^(?=.{1,255}$)[\dA-Za-z](?:(?:[\dA-Za-z]|-){0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:(?:[\dA-Za-z]|-){0,61}[\dA-Za-z])?)*\.?$/,
|
|
13
|
-
|
|
14
|
-
alphanumeric: /^[\dA-Za-z]+$/,
|
|
15
|
-
|
|
16
|
-
'utc-millisec': (input: string) => !Number.isNaN(+input),
|
|
17
|
-
|
|
18
|
-
alpha: /^[A-Za-z]+$/,
|
|
19
|
-
|
|
20
|
-
style: /\s*(.+?):\s*([^;]+);?/g,
|
|
21
|
-
|
|
22
|
-
phone: /^\+(?:\d ?){6,14}\d$/
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Generates JSON schema from JSON template/object.
|
|
27
|
-
*
|
|
28
|
-
* @export
|
|
29
|
-
* @param {Record<string, unknown>} json json object
|
|
30
|
-
* @param {Options} [options]
|
|
31
|
-
*
|
|
32
|
-
* @returns {*} {Record<string, unknown>}
|
|
33
|
-
*/
|
|
34
|
-
export function schemaFrom(
|
|
35
|
-
json: Record<string, unknown>,
|
|
36
|
-
options?: Options & { nullable?: boolean }
|
|
37
|
-
): Record<string, unknown> {
|
|
38
|
-
return toJsonSchema(json, {
|
|
39
|
-
required: false,
|
|
40
|
-
...options,
|
|
41
|
-
postProcessFnc: (type, schema, value, defaultFunc) => {
|
|
42
|
-
if (value !== json) {
|
|
43
|
-
schema.type = [type];
|
|
44
|
-
schema.nullable = options?.nullable ?? false;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return defaultFunc(type, schema, value);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Validates if a object is a JSON schema.
|
|
54
|
-
*
|
|
55
|
-
* @export
|
|
56
|
-
* @param {Record<string, unknown>} schema
|
|
57
|
-
*
|
|
58
|
-
* @returns {boolean} if is a JSON schema
|
|
59
|
-
*/
|
|
60
|
-
export function isJsonSchemaObject(
|
|
61
|
-
schema: Record<string, unknown>
|
|
62
|
-
): schema is JSONSchemaType<object> {
|
|
63
|
-
if (schema.type === 'object') return true;
|
|
64
|
-
|
|
65
|
-
return Array.isArray(schema.type) && schema.type.includes('object');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Converts a JSON schema to JSON template.
|
|
70
|
-
*
|
|
71
|
-
* @export
|
|
72
|
-
* @param {Record<string, unknown>} schema JSON schema
|
|
73
|
-
* @param {Record<string, any>} [container] template container
|
|
74
|
-
*
|
|
75
|
-
* @returns {unknown} object or default value
|
|
76
|
-
*/
|
|
77
|
-
export function schemaToJson(
|
|
78
|
-
schema: Record<string, unknown>,
|
|
79
|
-
container: Record<string, any> = {}
|
|
80
|
-
): unknown {
|
|
81
|
-
if (isJsonSchemaObject(schema)) {
|
|
82
|
-
for (const key in schema.properties)
|
|
83
|
-
container[key] = schemaToJson(schema.properties[key]);
|
|
84
|
-
|
|
85
|
-
return container;
|
|
86
|
-
} else {
|
|
87
|
-
return schema.default ?? (schema.nullable ? null : undefined);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Flatten a JSON schema.
|
|
93
|
-
*
|
|
94
|
-
* @export
|
|
95
|
-
* @param {Record<string, unknown>} schema JSON schema
|
|
96
|
-
* @param {string} [parentKey] previous level key
|
|
97
|
-
* @param {string} [nestingDelimiter] char for delimit nesting levels
|
|
98
|
-
* @param {Record<string, any>} [container] result container
|
|
99
|
-
*
|
|
100
|
-
* @returns {Record<string, unknown>} flattened schema
|
|
101
|
-
*/
|
|
102
|
-
export function flatSchema(
|
|
103
|
-
schema: Record<string, unknown>,
|
|
104
|
-
parentKey = '',
|
|
105
|
-
nestingDelimiter = '__',
|
|
106
|
-
container: Record<string, any> = {}
|
|
107
|
-
): Record<string, unknown> {
|
|
108
|
-
if (isJsonSchemaObject(schema)) {
|
|
109
|
-
for (const key in schema.properties) {
|
|
110
|
-
if (key[0] === '#') continue;
|
|
111
|
-
|
|
112
|
-
// global property, but prefix removed for injection
|
|
113
|
-
const subKey =
|
|
114
|
-
parentKey + (parentKey ? nestingDelimiter : '') + key;
|
|
115
|
-
|
|
116
|
-
container = {
|
|
117
|
-
...container,
|
|
118
|
-
...flatSchema(schema.properties[key], subKey, nestingDelimiter)
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return container;
|
|
123
|
-
} else {
|
|
124
|
-
return { [parentKey]: schema };
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Creates a JSON schema validator using AJV.
|
|
130
|
-
*
|
|
131
|
-
* @see https://ajv.js.org/
|
|
132
|
-
*
|
|
133
|
-
* @export
|
|
134
|
-
* @param {Record<string, object>} schema json schema by provider
|
|
135
|
-
* @param {boolean} enableFormats whether formats are enabled
|
|
136
|
-
*
|
|
137
|
-
* @returns {ValidateFunction} validators
|
|
138
|
-
*/
|
|
139
|
-
export function createValidator(
|
|
140
|
-
schema: Record<string, unknown>,
|
|
141
|
-
enableFormats = true
|
|
142
|
-
): ValidateFunction {
|
|
143
|
-
const ajv = new Ajv({
|
|
144
|
-
allErrors: true,
|
|
145
|
-
allowUnionTypes: true
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
if (enableFormats) {
|
|
149
|
-
addFormats(ajv, { mode: 'fast' });
|
|
150
|
-
|
|
151
|
-
for (const key in FORMAT_REGEXPS)
|
|
152
|
-
ajv.addFormat(key, FORMAT_REGEXPS[key]);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return ajv.compile(schema);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Creates a JSON schema validator lookup using AJV.
|
|
160
|
-
*
|
|
161
|
-
* @see https://ajv.js.org/
|
|
162
|
-
*
|
|
163
|
-
* @export
|
|
164
|
-
* @param {Record<string, object>} schemaLookup json schema by provider
|
|
165
|
-
* @param {boolean} enableFormats whether formats are enabled
|
|
166
|
-
*
|
|
167
|
-
* @returns {Record<string, ValidateFunction>} validators lookup
|
|
168
|
-
*/
|
|
169
|
-
export function createValidators(
|
|
170
|
-
schemaLookup: Record<string, object>,
|
|
171
|
-
enableFormats = true
|
|
172
|
-
): Record<string, ValidateFunction> {
|
|
173
|
-
const ajv = new Ajv({
|
|
174
|
-
allErrors: true,
|
|
175
|
-
allowUnionTypes: true
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
if (enableFormats) {
|
|
179
|
-
addFormats(ajv, { mode: 'fast' });
|
|
180
|
-
|
|
181
|
-
for (const key in FORMAT_REGEXPS)
|
|
182
|
-
ajv.addFormat(key, FORMAT_REGEXPS[key]);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const validators: Record<string, ValidateFunction> = {};
|
|
186
|
-
|
|
187
|
-
for (const key in schemaLookup)
|
|
188
|
-
validators[key] = ajv.compile(schemaLookup[key]);
|
|
189
|
-
|
|
190
|
-
return validators;
|
|
191
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"|DEFAULT|": {
|
|
3
|
-
"VAR1": "v1_default"
|
|
4
|
-
},
|
|
5
|
-
"|MODE|": {
|
|
6
|
-
"build": {
|
|
7
|
-
"NODE_ENV": "production"
|
|
8
|
-
},
|
|
9
|
-
"debug": {
|
|
10
|
-
"NODE_ENV": "development"
|
|
11
|
-
},
|
|
12
|
-
"test": {
|
|
13
|
-
"NODE_ENV": "test"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"|ENV|": {
|
|
17
|
-
"dev": {
|
|
18
|
-
"C1": "V1",
|
|
19
|
-
"C2": "V2",
|
|
20
|
-
"C3": 3,
|
|
21
|
-
"GROUP1": {
|
|
22
|
-
"VAR1": null,
|
|
23
|
-
"VAR2": "G1V2",
|
|
24
|
-
"VAR3": true,
|
|
25
|
-
"GROUP2": {
|
|
26
|
-
"VAR1": "G1G2V1"
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
"C4": "23"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
package/tests/env/dev.env.json
DELETED