@geekmidas/envkit 0.0.7 → 0.0.8
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/dist/{EnvironmentParser-C-arQEHQ.d.mts → EnvironmentParser-B8--woiB.d.cts} +40 -2
- package/dist/{EnvironmentParser-X4h2Vp4r.d.cts → EnvironmentParser-C_9v2BDw.d.mts} +40 -2
- package/dist/{EnvironmentParser-CQUOGqc0.mjs → EnvironmentParser-STvN_RCc.mjs} +46 -3
- package/dist/EnvironmentParser-STvN_RCc.mjs.map +1 -0
- package/dist/{EnvironmentParser-BDPDLv6i.cjs → EnvironmentParser-cnxuy7lw.cjs} +46 -3
- package/dist/EnvironmentParser-cnxuy7lw.cjs.map +1 -0
- package/dist/EnvironmentParser.cjs +1 -1
- package/dist/EnvironmentParser.d.cts +1 -1
- package/dist/EnvironmentParser.d.mts +1 -1
- package/dist/EnvironmentParser.mjs +1 -1
- package/dist/SnifferEnvironmentParser.cjs +140 -0
- package/dist/SnifferEnvironmentParser.cjs.map +1 -0
- package/dist/SnifferEnvironmentParser.d.cts +50 -0
- package/dist/SnifferEnvironmentParser.d.mts +50 -0
- package/dist/SnifferEnvironmentParser.mjs +139 -0
- package/dist/SnifferEnvironmentParser.mjs.map +1 -0
- package/dist/index.cjs +2 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/sst.cjs +131 -4
- package/dist/sst.cjs.map +1 -0
- package/dist/sst.d.cts +2 -1
- package/dist/sst.d.mts +2 -1
- package/dist/sst.mjs +128 -2
- package/dist/sst.mjs.map +1 -0
- package/package.json +6 -1
- package/src/EnvironmentParser.ts +51 -2
- package/src/SnifferEnvironmentParser.ts +207 -0
- package/src/__tests__/EnvironmentParser.spec.ts +147 -0
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +332 -0
- package/src/index.ts +1 -1
- package/dist/__tests__/ConfigParser.spec.cjs +0 -323
- package/dist/__tests__/ConfigParser.spec.d.cts +0 -1
- package/dist/__tests__/ConfigParser.spec.d.mts +0 -1
- package/dist/__tests__/ConfigParser.spec.mjs +0 -322
- package/dist/__tests__/EnvironmentParser.spec.cjs +0 -422
- package/dist/__tests__/EnvironmentParser.spec.d.cts +0 -1
- package/dist/__tests__/EnvironmentParser.spec.d.mts +0 -1
- package/dist/__tests__/EnvironmentParser.spec.mjs +0 -421
- package/dist/__tests__/sst.spec.cjs +0 -305
- package/dist/__tests__/sst.spec.d.cts +0 -1
- package/dist/__tests__/sst.spec.d.mts +0 -1
- package/dist/__tests__/sst.spec.mjs +0 -304
- package/dist/sst-BSxwaAdz.cjs +0 -146
- package/dist/sst-CQhO0S6y.mjs +0 -128
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_EnvironmentParser = require('../EnvironmentParser-BDPDLv6i.cjs');
|
|
3
|
-
const zod_v4 = require_chunk.__toESM(require("zod/v4"));
|
|
4
|
-
const vitest = require_chunk.__toESM(require("vitest"));
|
|
5
|
-
|
|
6
|
-
//#region src/__tests__/ConfigParser.spec.ts
|
|
7
|
-
(0, vitest.describe)("ConfigParser", () => {
|
|
8
|
-
(0, vitest.describe)("Basic functionality", () => {
|
|
9
|
-
(0, vitest.it)("should parse simple Zod schemas", () => {
|
|
10
|
-
const config = {
|
|
11
|
-
name: zod_v4.z.string().default("Test"),
|
|
12
|
-
age: zod_v4.z.number().default(25),
|
|
13
|
-
active: zod_v4.z.boolean().default(true)
|
|
14
|
-
};
|
|
15
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
16
|
-
const result = parser.parse();
|
|
17
|
-
(0, vitest.expect)(result).toEqual({
|
|
18
|
-
name: "Test",
|
|
19
|
-
age: 25,
|
|
20
|
-
active: true
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
(0, vitest.it)("should handle optional values", () => {
|
|
24
|
-
const config = {
|
|
25
|
-
required: zod_v4.z.string().default("value"),
|
|
26
|
-
optional: zod_v4.z.string().optional()
|
|
27
|
-
};
|
|
28
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
29
|
-
const result = parser.parse();
|
|
30
|
-
(0, vitest.expect)(result).toEqual({
|
|
31
|
-
required: "value",
|
|
32
|
-
optional: void 0
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
(0, vitest.it)("should validate and use provided default values", () => {
|
|
36
|
-
const config = {
|
|
37
|
-
port: zod_v4.z.number().default(3e3),
|
|
38
|
-
host: zod_v4.z.string().default("localhost"),
|
|
39
|
-
debug: zod_v4.z.boolean().default(false)
|
|
40
|
-
};
|
|
41
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
42
|
-
const result = parser.parse();
|
|
43
|
-
(0, vitest.expect)(result).toEqual({
|
|
44
|
-
port: 3e3,
|
|
45
|
-
host: "localhost",
|
|
46
|
-
debug: false
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
(0, vitest.describe)("Nested objects", () => {
|
|
51
|
-
(0, vitest.it)("should parse nested configuration objects", () => {
|
|
52
|
-
const config = {
|
|
53
|
-
database: {
|
|
54
|
-
host: zod_v4.z.string().default("localhost"),
|
|
55
|
-
port: zod_v4.z.number().default(5432),
|
|
56
|
-
ssl: zod_v4.z.boolean().default(false)
|
|
57
|
-
},
|
|
58
|
-
api: {
|
|
59
|
-
key: zod_v4.z.string().default("default-key"),
|
|
60
|
-
timeout: zod_v4.z.number().default(5e3)
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
64
|
-
const result = parser.parse();
|
|
65
|
-
(0, vitest.expect)(result).toEqual({
|
|
66
|
-
database: {
|
|
67
|
-
host: "localhost",
|
|
68
|
-
port: 5432,
|
|
69
|
-
ssl: false
|
|
70
|
-
},
|
|
71
|
-
api: {
|
|
72
|
-
key: "default-key",
|
|
73
|
-
timeout: 5e3
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
(0, vitest.it)("should handle deeply nested objects", () => {
|
|
78
|
-
const config = { app: {
|
|
79
|
-
name: zod_v4.z.string().default("MyApp"),
|
|
80
|
-
version: zod_v4.z.string().default("1.0.0"),
|
|
81
|
-
features: {
|
|
82
|
-
auth: {
|
|
83
|
-
enabled: zod_v4.z.boolean().default(true),
|
|
84
|
-
provider: zod_v4.z.string().default("local")
|
|
85
|
-
},
|
|
86
|
-
cache: {
|
|
87
|
-
enabled: zod_v4.z.boolean().default(false),
|
|
88
|
-
ttl: zod_v4.z.number().default(3600)
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
} };
|
|
92
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
93
|
-
const result = parser.parse();
|
|
94
|
-
(0, vitest.expect)(result).toEqual({ app: {
|
|
95
|
-
name: "MyApp",
|
|
96
|
-
version: "1.0.0",
|
|
97
|
-
features: {
|
|
98
|
-
auth: {
|
|
99
|
-
enabled: true,
|
|
100
|
-
provider: "local"
|
|
101
|
-
},
|
|
102
|
-
cache: {
|
|
103
|
-
enabled: false,
|
|
104
|
-
ttl: 3600
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
} });
|
|
108
|
-
});
|
|
109
|
-
(0, vitest.it)("should handle mixed nested and flat configuration", () => {
|
|
110
|
-
const config = {
|
|
111
|
-
appName: zod_v4.z.string().default("Test App"),
|
|
112
|
-
database: {
|
|
113
|
-
url: zod_v4.z.string().default("postgres://localhost/test"),
|
|
114
|
-
poolSize: zod_v4.z.number().default(10)
|
|
115
|
-
},
|
|
116
|
-
port: zod_v4.z.number().default(3e3),
|
|
117
|
-
features: { logging: {
|
|
118
|
-
level: zod_v4.z.string().default("info"),
|
|
119
|
-
pretty: zod_v4.z.boolean().default(true)
|
|
120
|
-
} }
|
|
121
|
-
};
|
|
122
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
123
|
-
const result = parser.parse();
|
|
124
|
-
(0, vitest.expect)(result).toEqual({
|
|
125
|
-
appName: "Test App",
|
|
126
|
-
database: {
|
|
127
|
-
url: "postgres://localhost/test",
|
|
128
|
-
poolSize: 10
|
|
129
|
-
},
|
|
130
|
-
port: 3e3,
|
|
131
|
-
features: { logging: {
|
|
132
|
-
level: "info",
|
|
133
|
-
pretty: true
|
|
134
|
-
} }
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
(0, vitest.describe)("Error handling", () => {
|
|
139
|
-
(0, vitest.it)("should throw ZodError for schemas without defaults", () => {
|
|
140
|
-
const config = {
|
|
141
|
-
required: zod_v4.z.string(),
|
|
142
|
-
alsoRequired: zod_v4.z.number()
|
|
143
|
-
};
|
|
144
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
145
|
-
(0, vitest.expect)(() => parser.parse()).toThrow(zod_v4.z.ZodError);
|
|
146
|
-
});
|
|
147
|
-
(0, vitest.it)("should collect multiple validation errors", () => {
|
|
148
|
-
const config = {
|
|
149
|
-
field1: zod_v4.z.string(),
|
|
150
|
-
field2: zod_v4.z.number(),
|
|
151
|
-
field3: zod_v4.z.boolean()
|
|
152
|
-
};
|
|
153
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
154
|
-
try {
|
|
155
|
-
parser.parse();
|
|
156
|
-
(0, vitest.expect)(true).toBe(false);
|
|
157
|
-
} catch (error) {
|
|
158
|
-
(0, vitest.expect)(error).toBeInstanceOf(zod_v4.z.ZodError);
|
|
159
|
-
const zodError = error;
|
|
160
|
-
(0, vitest.expect)(zodError.issues).toHaveLength(3);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
(0, vitest.it)("should include correct paths in nested validation errors", () => {
|
|
164
|
-
const config = {
|
|
165
|
-
database: {
|
|
166
|
-
host: zod_v4.z.string(),
|
|
167
|
-
port: zod_v4.z.number()
|
|
168
|
-
},
|
|
169
|
-
api: { key: zod_v4.z.string() }
|
|
170
|
-
};
|
|
171
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
172
|
-
try {
|
|
173
|
-
parser.parse();
|
|
174
|
-
(0, vitest.expect)(true).toBe(false);
|
|
175
|
-
} catch (error) {
|
|
176
|
-
(0, vitest.expect)(error).toBeInstanceOf(zod_v4.z.ZodError);
|
|
177
|
-
const zodError = error;
|
|
178
|
-
const paths = zodError.issues.map((err) => err.path.join("."));
|
|
179
|
-
(0, vitest.expect)(paths).toContain("database.host");
|
|
180
|
-
(0, vitest.expect)(paths).toContain("database.port");
|
|
181
|
-
(0, vitest.expect)(paths).toContain("api.key");
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
(0, vitest.it)("should use default values that pass validation", () => {
|
|
185
|
-
const config = {
|
|
186
|
-
port: zod_v4.z.number().min(1e3).max(65535).default(3e3),
|
|
187
|
-
email: zod_v4.z.string().email().default("admin@example.com")
|
|
188
|
-
};
|
|
189
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
190
|
-
const result = parser.parse();
|
|
191
|
-
(0, vitest.expect)(result).toEqual({
|
|
192
|
-
port: 3e3,
|
|
193
|
-
email: "admin@example.com"
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
(0, vitest.describe)("Type safety", () => {
|
|
198
|
-
(0, vitest.it)("should infer correct types for simple configuration", () => {
|
|
199
|
-
const config = {
|
|
200
|
-
name: zod_v4.z.string().default("test"),
|
|
201
|
-
count: zod_v4.z.number().default(42),
|
|
202
|
-
enabled: zod_v4.z.boolean().default(true)
|
|
203
|
-
};
|
|
204
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
205
|
-
const result = parser.parse();
|
|
206
|
-
const _typeCheck = true;
|
|
207
|
-
const _typeCheck2 = true;
|
|
208
|
-
(0, vitest.expect)(_typeCheck).toBe(true);
|
|
209
|
-
(0, vitest.expect)(_typeCheck2).toBe(true);
|
|
210
|
-
});
|
|
211
|
-
(0, vitest.it)("should infer correct types for nested configuration", () => {
|
|
212
|
-
const config = {
|
|
213
|
-
database: {
|
|
214
|
-
host: zod_v4.z.string().default("localhost"),
|
|
215
|
-
port: zod_v4.z.number().default(5432)
|
|
216
|
-
},
|
|
217
|
-
features: { auth: zod_v4.z.boolean().default(true) }
|
|
218
|
-
};
|
|
219
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
220
|
-
const result = parser.parse();
|
|
221
|
-
const _typeCheck = true;
|
|
222
|
-
const _typeCheck2 = true;
|
|
223
|
-
(0, vitest.expect)(_typeCheck).toBe(true);
|
|
224
|
-
(0, vitest.expect)(_typeCheck2).toBe(true);
|
|
225
|
-
});
|
|
226
|
-
(0, vitest.it)("should handle optional types correctly", () => {
|
|
227
|
-
const config = {
|
|
228
|
-
required: zod_v4.z.string().default("value"),
|
|
229
|
-
optional: zod_v4.z.string().optional(),
|
|
230
|
-
nullable: zod_v4.z.string().nullable().default(null)
|
|
231
|
-
};
|
|
232
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
233
|
-
const result = parser.parse();
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
(0, vitest.describe)("Complex schemas", () => {
|
|
237
|
-
(0, vitest.it)("should handle enum schemas", () => {
|
|
238
|
-
const config = {
|
|
239
|
-
environment: zod_v4.z.enum([
|
|
240
|
-
"development",
|
|
241
|
-
"staging",
|
|
242
|
-
"production"
|
|
243
|
-
]).default("development"),
|
|
244
|
-
logLevel: zod_v4.z.enum([
|
|
245
|
-
"debug",
|
|
246
|
-
"info",
|
|
247
|
-
"warn",
|
|
248
|
-
"error"
|
|
249
|
-
]).default("info")
|
|
250
|
-
};
|
|
251
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
252
|
-
const result = parser.parse();
|
|
253
|
-
(0, vitest.expect)(result).toEqual({
|
|
254
|
-
environment: "development",
|
|
255
|
-
logLevel: "info"
|
|
256
|
-
});
|
|
257
|
-
});
|
|
258
|
-
(0, vitest.it)("should handle union schemas", () => {
|
|
259
|
-
const config = {
|
|
260
|
-
port: zod_v4.z.union([zod_v4.z.string(), zod_v4.z.number()]).default(3e3),
|
|
261
|
-
timeout: zod_v4.z.union([zod_v4.z.number(), zod_v4.z.null()]).default(null)
|
|
262
|
-
};
|
|
263
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
264
|
-
const result = parser.parse();
|
|
265
|
-
(0, vitest.expect)(result).toEqual({
|
|
266
|
-
port: 3e3,
|
|
267
|
-
timeout: null
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
(0, vitest.it)("should handle array schemas", () => {
|
|
271
|
-
const config = {
|
|
272
|
-
tags: zod_v4.z.array(zod_v4.z.string()).default(["tag1", "tag2"]),
|
|
273
|
-
ports: zod_v4.z.array(zod_v4.z.number()).default([3e3, 3001])
|
|
274
|
-
};
|
|
275
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
276
|
-
const result = parser.parse();
|
|
277
|
-
(0, vitest.expect)(result).toEqual({
|
|
278
|
-
tags: ["tag1", "tag2"],
|
|
279
|
-
ports: [3e3, 3001]
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
(0, vitest.it)("should handle record schemas", () => {
|
|
283
|
-
const config = {
|
|
284
|
-
metadata: zod_v4.z.record(zod_v4.z.string(), zod_v4.z.string()).default({
|
|
285
|
-
key1: "value1",
|
|
286
|
-
key2: "value2"
|
|
287
|
-
}),
|
|
288
|
-
counters: zod_v4.z.record(zod_v4.z.string(), zod_v4.z.number()).default({
|
|
289
|
-
count1: 1,
|
|
290
|
-
count2: 2
|
|
291
|
-
})
|
|
292
|
-
};
|
|
293
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
294
|
-
const result = parser.parse();
|
|
295
|
-
(0, vitest.expect)(result).toEqual({
|
|
296
|
-
metadata: {
|
|
297
|
-
key1: "value1",
|
|
298
|
-
key2: "value2"
|
|
299
|
-
},
|
|
300
|
-
counters: {
|
|
301
|
-
count1: 1,
|
|
302
|
-
count2: 2
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
(0, vitest.it)("should handle transformed schemas", () => {
|
|
307
|
-
const config = {
|
|
308
|
-
portString: zod_v4.z.string().transform(Number).default(8080),
|
|
309
|
-
booleanString: zod_v4.z.string().transform((v) => v === "true").default(false),
|
|
310
|
-
jsonString: zod_v4.z.string().transform((v) => JSON.parse(v)).default({ key: "value" })
|
|
311
|
-
};
|
|
312
|
-
const parser = new require_EnvironmentParser.ConfigParser(config);
|
|
313
|
-
const result = parser.parse();
|
|
314
|
-
(0, vitest.expect)(result).toEqual({
|
|
315
|
-
portString: 8080,
|
|
316
|
-
booleanString: false,
|
|
317
|
-
jsonString: { key: "value" }
|
|
318
|
-
});
|
|
319
|
-
});
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
//#endregion
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
import { ConfigParser } from "../EnvironmentParser-CQUOGqc0.mjs";
|
|
2
|
-
import { z } from "zod/v4";
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
4
|
-
|
|
5
|
-
//#region src/__tests__/ConfigParser.spec.ts
|
|
6
|
-
describe("ConfigParser", () => {
|
|
7
|
-
describe("Basic functionality", () => {
|
|
8
|
-
it("should parse simple Zod schemas", () => {
|
|
9
|
-
const config = {
|
|
10
|
-
name: z.string().default("Test"),
|
|
11
|
-
age: z.number().default(25),
|
|
12
|
-
active: z.boolean().default(true)
|
|
13
|
-
};
|
|
14
|
-
const parser = new ConfigParser(config);
|
|
15
|
-
const result = parser.parse();
|
|
16
|
-
expect(result).toEqual({
|
|
17
|
-
name: "Test",
|
|
18
|
-
age: 25,
|
|
19
|
-
active: true
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
it("should handle optional values", () => {
|
|
23
|
-
const config = {
|
|
24
|
-
required: z.string().default("value"),
|
|
25
|
-
optional: z.string().optional()
|
|
26
|
-
};
|
|
27
|
-
const parser = new ConfigParser(config);
|
|
28
|
-
const result = parser.parse();
|
|
29
|
-
expect(result).toEqual({
|
|
30
|
-
required: "value",
|
|
31
|
-
optional: void 0
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
it("should validate and use provided default values", () => {
|
|
35
|
-
const config = {
|
|
36
|
-
port: z.number().default(3e3),
|
|
37
|
-
host: z.string().default("localhost"),
|
|
38
|
-
debug: z.boolean().default(false)
|
|
39
|
-
};
|
|
40
|
-
const parser = new ConfigParser(config);
|
|
41
|
-
const result = parser.parse();
|
|
42
|
-
expect(result).toEqual({
|
|
43
|
-
port: 3e3,
|
|
44
|
-
host: "localhost",
|
|
45
|
-
debug: false
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
describe("Nested objects", () => {
|
|
50
|
-
it("should parse nested configuration objects", () => {
|
|
51
|
-
const config = {
|
|
52
|
-
database: {
|
|
53
|
-
host: z.string().default("localhost"),
|
|
54
|
-
port: z.number().default(5432),
|
|
55
|
-
ssl: z.boolean().default(false)
|
|
56
|
-
},
|
|
57
|
-
api: {
|
|
58
|
-
key: z.string().default("default-key"),
|
|
59
|
-
timeout: z.number().default(5e3)
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
const parser = new ConfigParser(config);
|
|
63
|
-
const result = parser.parse();
|
|
64
|
-
expect(result).toEqual({
|
|
65
|
-
database: {
|
|
66
|
-
host: "localhost",
|
|
67
|
-
port: 5432,
|
|
68
|
-
ssl: false
|
|
69
|
-
},
|
|
70
|
-
api: {
|
|
71
|
-
key: "default-key",
|
|
72
|
-
timeout: 5e3
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
it("should handle deeply nested objects", () => {
|
|
77
|
-
const config = { app: {
|
|
78
|
-
name: z.string().default("MyApp"),
|
|
79
|
-
version: z.string().default("1.0.0"),
|
|
80
|
-
features: {
|
|
81
|
-
auth: {
|
|
82
|
-
enabled: z.boolean().default(true),
|
|
83
|
-
provider: z.string().default("local")
|
|
84
|
-
},
|
|
85
|
-
cache: {
|
|
86
|
-
enabled: z.boolean().default(false),
|
|
87
|
-
ttl: z.number().default(3600)
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
} };
|
|
91
|
-
const parser = new ConfigParser(config);
|
|
92
|
-
const result = parser.parse();
|
|
93
|
-
expect(result).toEqual({ app: {
|
|
94
|
-
name: "MyApp",
|
|
95
|
-
version: "1.0.0",
|
|
96
|
-
features: {
|
|
97
|
-
auth: {
|
|
98
|
-
enabled: true,
|
|
99
|
-
provider: "local"
|
|
100
|
-
},
|
|
101
|
-
cache: {
|
|
102
|
-
enabled: false,
|
|
103
|
-
ttl: 3600
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
} });
|
|
107
|
-
});
|
|
108
|
-
it("should handle mixed nested and flat configuration", () => {
|
|
109
|
-
const config = {
|
|
110
|
-
appName: z.string().default("Test App"),
|
|
111
|
-
database: {
|
|
112
|
-
url: z.string().default("postgres://localhost/test"),
|
|
113
|
-
poolSize: z.number().default(10)
|
|
114
|
-
},
|
|
115
|
-
port: z.number().default(3e3),
|
|
116
|
-
features: { logging: {
|
|
117
|
-
level: z.string().default("info"),
|
|
118
|
-
pretty: z.boolean().default(true)
|
|
119
|
-
} }
|
|
120
|
-
};
|
|
121
|
-
const parser = new ConfigParser(config);
|
|
122
|
-
const result = parser.parse();
|
|
123
|
-
expect(result).toEqual({
|
|
124
|
-
appName: "Test App",
|
|
125
|
-
database: {
|
|
126
|
-
url: "postgres://localhost/test",
|
|
127
|
-
poolSize: 10
|
|
128
|
-
},
|
|
129
|
-
port: 3e3,
|
|
130
|
-
features: { logging: {
|
|
131
|
-
level: "info",
|
|
132
|
-
pretty: true
|
|
133
|
-
} }
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
describe("Error handling", () => {
|
|
138
|
-
it("should throw ZodError for schemas without defaults", () => {
|
|
139
|
-
const config = {
|
|
140
|
-
required: z.string(),
|
|
141
|
-
alsoRequired: z.number()
|
|
142
|
-
};
|
|
143
|
-
const parser = new ConfigParser(config);
|
|
144
|
-
expect(() => parser.parse()).toThrow(z.ZodError);
|
|
145
|
-
});
|
|
146
|
-
it("should collect multiple validation errors", () => {
|
|
147
|
-
const config = {
|
|
148
|
-
field1: z.string(),
|
|
149
|
-
field2: z.number(),
|
|
150
|
-
field3: z.boolean()
|
|
151
|
-
};
|
|
152
|
-
const parser = new ConfigParser(config);
|
|
153
|
-
try {
|
|
154
|
-
parser.parse();
|
|
155
|
-
expect(true).toBe(false);
|
|
156
|
-
} catch (error) {
|
|
157
|
-
expect(error).toBeInstanceOf(z.ZodError);
|
|
158
|
-
const zodError = error;
|
|
159
|
-
expect(zodError.issues).toHaveLength(3);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
it("should include correct paths in nested validation errors", () => {
|
|
163
|
-
const config = {
|
|
164
|
-
database: {
|
|
165
|
-
host: z.string(),
|
|
166
|
-
port: z.number()
|
|
167
|
-
},
|
|
168
|
-
api: { key: z.string() }
|
|
169
|
-
};
|
|
170
|
-
const parser = new ConfigParser(config);
|
|
171
|
-
try {
|
|
172
|
-
parser.parse();
|
|
173
|
-
expect(true).toBe(false);
|
|
174
|
-
} catch (error) {
|
|
175
|
-
expect(error).toBeInstanceOf(z.ZodError);
|
|
176
|
-
const zodError = error;
|
|
177
|
-
const paths = zodError.issues.map((err) => err.path.join("."));
|
|
178
|
-
expect(paths).toContain("database.host");
|
|
179
|
-
expect(paths).toContain("database.port");
|
|
180
|
-
expect(paths).toContain("api.key");
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
it("should use default values that pass validation", () => {
|
|
184
|
-
const config = {
|
|
185
|
-
port: z.number().min(1e3).max(65535).default(3e3),
|
|
186
|
-
email: z.string().email().default("admin@example.com")
|
|
187
|
-
};
|
|
188
|
-
const parser = new ConfigParser(config);
|
|
189
|
-
const result = parser.parse();
|
|
190
|
-
expect(result).toEqual({
|
|
191
|
-
port: 3e3,
|
|
192
|
-
email: "admin@example.com"
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
describe("Type safety", () => {
|
|
197
|
-
it("should infer correct types for simple configuration", () => {
|
|
198
|
-
const config = {
|
|
199
|
-
name: z.string().default("test"),
|
|
200
|
-
count: z.number().default(42),
|
|
201
|
-
enabled: z.boolean().default(true)
|
|
202
|
-
};
|
|
203
|
-
const parser = new ConfigParser(config);
|
|
204
|
-
const result = parser.parse();
|
|
205
|
-
const _typeCheck = true;
|
|
206
|
-
const _typeCheck2 = true;
|
|
207
|
-
expect(_typeCheck).toBe(true);
|
|
208
|
-
expect(_typeCheck2).toBe(true);
|
|
209
|
-
});
|
|
210
|
-
it("should infer correct types for nested configuration", () => {
|
|
211
|
-
const config = {
|
|
212
|
-
database: {
|
|
213
|
-
host: z.string().default("localhost"),
|
|
214
|
-
port: z.number().default(5432)
|
|
215
|
-
},
|
|
216
|
-
features: { auth: z.boolean().default(true) }
|
|
217
|
-
};
|
|
218
|
-
const parser = new ConfigParser(config);
|
|
219
|
-
const result = parser.parse();
|
|
220
|
-
const _typeCheck = true;
|
|
221
|
-
const _typeCheck2 = true;
|
|
222
|
-
expect(_typeCheck).toBe(true);
|
|
223
|
-
expect(_typeCheck2).toBe(true);
|
|
224
|
-
});
|
|
225
|
-
it("should handle optional types correctly", () => {
|
|
226
|
-
const config = {
|
|
227
|
-
required: z.string().default("value"),
|
|
228
|
-
optional: z.string().optional(),
|
|
229
|
-
nullable: z.string().nullable().default(null)
|
|
230
|
-
};
|
|
231
|
-
const parser = new ConfigParser(config);
|
|
232
|
-
const result = parser.parse();
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
describe("Complex schemas", () => {
|
|
236
|
-
it("should handle enum schemas", () => {
|
|
237
|
-
const config = {
|
|
238
|
-
environment: z.enum([
|
|
239
|
-
"development",
|
|
240
|
-
"staging",
|
|
241
|
-
"production"
|
|
242
|
-
]).default("development"),
|
|
243
|
-
logLevel: z.enum([
|
|
244
|
-
"debug",
|
|
245
|
-
"info",
|
|
246
|
-
"warn",
|
|
247
|
-
"error"
|
|
248
|
-
]).default("info")
|
|
249
|
-
};
|
|
250
|
-
const parser = new ConfigParser(config);
|
|
251
|
-
const result = parser.parse();
|
|
252
|
-
expect(result).toEqual({
|
|
253
|
-
environment: "development",
|
|
254
|
-
logLevel: "info"
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
it("should handle union schemas", () => {
|
|
258
|
-
const config = {
|
|
259
|
-
port: z.union([z.string(), z.number()]).default(3e3),
|
|
260
|
-
timeout: z.union([z.number(), z.null()]).default(null)
|
|
261
|
-
};
|
|
262
|
-
const parser = new ConfigParser(config);
|
|
263
|
-
const result = parser.parse();
|
|
264
|
-
expect(result).toEqual({
|
|
265
|
-
port: 3e3,
|
|
266
|
-
timeout: null
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
it("should handle array schemas", () => {
|
|
270
|
-
const config = {
|
|
271
|
-
tags: z.array(z.string()).default(["tag1", "tag2"]),
|
|
272
|
-
ports: z.array(z.number()).default([3e3, 3001])
|
|
273
|
-
};
|
|
274
|
-
const parser = new ConfigParser(config);
|
|
275
|
-
const result = parser.parse();
|
|
276
|
-
expect(result).toEqual({
|
|
277
|
-
tags: ["tag1", "tag2"],
|
|
278
|
-
ports: [3e3, 3001]
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
it("should handle record schemas", () => {
|
|
282
|
-
const config = {
|
|
283
|
-
metadata: z.record(z.string(), z.string()).default({
|
|
284
|
-
key1: "value1",
|
|
285
|
-
key2: "value2"
|
|
286
|
-
}),
|
|
287
|
-
counters: z.record(z.string(), z.number()).default({
|
|
288
|
-
count1: 1,
|
|
289
|
-
count2: 2
|
|
290
|
-
})
|
|
291
|
-
};
|
|
292
|
-
const parser = new ConfigParser(config);
|
|
293
|
-
const result = parser.parse();
|
|
294
|
-
expect(result).toEqual({
|
|
295
|
-
metadata: {
|
|
296
|
-
key1: "value1",
|
|
297
|
-
key2: "value2"
|
|
298
|
-
},
|
|
299
|
-
counters: {
|
|
300
|
-
count1: 1,
|
|
301
|
-
count2: 2
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
it("should handle transformed schemas", () => {
|
|
306
|
-
const config = {
|
|
307
|
-
portString: z.string().transform(Number).default(8080),
|
|
308
|
-
booleanString: z.string().transform((v) => v === "true").default(false),
|
|
309
|
-
jsonString: z.string().transform((v) => JSON.parse(v)).default({ key: "value" })
|
|
310
|
-
};
|
|
311
|
-
const parser = new ConfigParser(config);
|
|
312
|
-
const result = parser.parse();
|
|
313
|
-
expect(result).toEqual({
|
|
314
|
-
portString: 8080,
|
|
315
|
-
booleanString: false,
|
|
316
|
-
jsonString: { key: "value" }
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
});
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
//#endregion
|