@geekmidas/envkit 0.0.2 → 0.0.4
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-Bo2CCl_K.cjs +178 -0
- package/dist/EnvironmentParser-jKrGMBhP.mjs +166 -0
- package/dist/EnvironmentParser.cjs +1 -1
- package/dist/EnvironmentParser.mjs +1 -1
- package/dist/__tests__/ConfigParser.spec.cjs +323 -0
- package/dist/__tests__/ConfigParser.spec.mjs +322 -0
- package/dist/__tests__/EnvironmentParser.spec.cjs +422 -0
- package/dist/__tests__/EnvironmentParser.spec.mjs +421 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sst.cjs +131 -0
- package/dist/sst.mjs +128 -0
- package/package.json +10 -3
- package/src/EnvironmentParser.ts +166 -49
- package/src/__tests__/ConfigParser.spec.ts +394 -0
- package/src/__tests__/EnvironmentParser.spec.ts +692 -0
- package/src/sst.ts +221 -0
- package/dist/EnvironmentParser-Dd6TbwJC.mjs +0 -99
- package/dist/EnvironmentParser-nZnZXM_Y.cjs +0 -133
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const lodash_get = require_chunk.__toESM(require("lodash.get"));
|
|
3
|
+
const lodash_set = require_chunk.__toESM(require("lodash.set"));
|
|
4
|
+
const zod_v4 = require_chunk.__toESM(require("zod/v4"));
|
|
5
|
+
|
|
6
|
+
//#region src/EnvironmentParser.ts
|
|
7
|
+
/**
|
|
8
|
+
* Parses and validates configuration objects against Zod schemas.
|
|
9
|
+
* Handles nested configurations and aggregates validation errors.
|
|
10
|
+
*
|
|
11
|
+
* @template TResponse - The shape of the configuration object
|
|
12
|
+
*/
|
|
13
|
+
var ConfigParser = class {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new ConfigParser instance.
|
|
16
|
+
*
|
|
17
|
+
* @param config - The configuration object to parse
|
|
18
|
+
*/
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.config = config;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Parses the config object and validates it against the Zod schemas
|
|
24
|
+
* @returns The parsed config object
|
|
25
|
+
*/
|
|
26
|
+
parse() {
|
|
27
|
+
const errors = [];
|
|
28
|
+
const parseDeep = (config, path = []) => {
|
|
29
|
+
const result = {};
|
|
30
|
+
if (config && typeof config !== "object") return config;
|
|
31
|
+
for (const key in config) {
|
|
32
|
+
const schema = config[key];
|
|
33
|
+
const currentPath = [...path, key];
|
|
34
|
+
if (schema instanceof zod_v4.z.ZodType) {
|
|
35
|
+
const parsed = schema.safeParse(void 0);
|
|
36
|
+
if (parsed.success) (0, lodash_set.default)(result, key, parsed.data);
|
|
37
|
+
else errors.push(...parsed.error.issues.map((issue) => ({
|
|
38
|
+
...issue,
|
|
39
|
+
path: [...currentPath, ...issue.path]
|
|
40
|
+
})));
|
|
41
|
+
} else if (schema) (0, lodash_set.default)(result, key, parseDeep(schema, currentPath));
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
const parsedConfig = parseDeep(this.config);
|
|
46
|
+
if (errors.length > 0) throw new zod_v4.z.ZodError(errors);
|
|
47
|
+
return parsedConfig;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Parses environment variables with type-safe validation using Zod schemas.
|
|
52
|
+
* Provides a fluent API for defining environment variable schemas with automatic
|
|
53
|
+
* error context enrichment.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The type of the configuration object (typically process.env)
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const config = new EnvironmentParser(process.env)
|
|
60
|
+
* .create((get) => ({
|
|
61
|
+
* port: get('PORT').string().transform(Number).default(3000),
|
|
62
|
+
* database: {
|
|
63
|
+
* url: get('DATABASE_URL').string().url()
|
|
64
|
+
* }
|
|
65
|
+
* }))
|
|
66
|
+
* .parse();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
var EnvironmentParser = class {
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new EnvironmentParser instance.
|
|
72
|
+
*
|
|
73
|
+
* @param config - The configuration object to parse (typically process.env)
|
|
74
|
+
*/
|
|
75
|
+
constructor(config) {
|
|
76
|
+
this.config = config;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages
|
|
80
|
+
* with environment variable context.
|
|
81
|
+
*
|
|
82
|
+
* @param schema - The Zod schema to wrap
|
|
83
|
+
* @param name - The environment variable name for error context
|
|
84
|
+
* @returns A wrapped Zod schema with enhanced error reporting
|
|
85
|
+
*/
|
|
86
|
+
wrapSchema = (schema, name) => {
|
|
87
|
+
return new Proxy(schema, { get: (target, prop) => {
|
|
88
|
+
if (prop === "parse") return () => {
|
|
89
|
+
const value = (0, lodash_get.default)(this.config, name);
|
|
90
|
+
try {
|
|
91
|
+
return target.parse(value);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof zod_v4.z.ZodError) {
|
|
94
|
+
const modifiedIssues = error.issues.map((issue) => ({
|
|
95
|
+
...issue,
|
|
96
|
+
message: `Environment variable "${name}": ${issue.message}`,
|
|
97
|
+
path: [name, ...issue.path]
|
|
98
|
+
}));
|
|
99
|
+
throw new zod_v4.z.ZodError(modifiedIssues);
|
|
100
|
+
}
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
if (prop === "safeParse") return () => {
|
|
105
|
+
const value = (0, lodash_get.default)(this.config, name);
|
|
106
|
+
const result = target.safeParse(value);
|
|
107
|
+
if (!result.success) {
|
|
108
|
+
const modifiedIssues = result.error.issues.map((issue) => ({
|
|
109
|
+
...issue,
|
|
110
|
+
message: `Environment variable "${name}": ${issue.message}`,
|
|
111
|
+
path: [name, ...issue.path]
|
|
112
|
+
}));
|
|
113
|
+
return {
|
|
114
|
+
success: false,
|
|
115
|
+
error: new zod_v4.z.ZodError(modifiedIssues)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
120
|
+
const originalProp = target[prop];
|
|
121
|
+
if (typeof originalProp === "function") return (...args) => {
|
|
122
|
+
const result = originalProp.apply(target, args);
|
|
123
|
+
if (result && typeof result === "object" && "parse" in result) return this.wrapSchema(result, name);
|
|
124
|
+
return result;
|
|
125
|
+
};
|
|
126
|
+
return originalProp;
|
|
127
|
+
} });
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Creates a proxied version of the Zod object that wraps all schema creators
|
|
131
|
+
* to provide enhanced error messages with environment variable context.
|
|
132
|
+
*
|
|
133
|
+
* @param name - The environment variable name
|
|
134
|
+
* @returns A proxied Zod object with wrapped schema creators
|
|
135
|
+
*/
|
|
136
|
+
getZodGetter = (name) => {
|
|
137
|
+
return new Proxy({ ...zod_v4.z }, { get: (target, prop) => {
|
|
138
|
+
const value = target[prop];
|
|
139
|
+
if (typeof value === "function") return (...args) => {
|
|
140
|
+
const schema = value(...args);
|
|
141
|
+
return this.wrapSchema(schema, name);
|
|
142
|
+
};
|
|
143
|
+
if (value && typeof value === "object") return new Proxy(value, { get: (nestedTarget, nestedProp) => {
|
|
144
|
+
const nestedValue = nestedTarget[nestedProp];
|
|
145
|
+
if (typeof nestedValue === "function") return (...args) => {
|
|
146
|
+
const schema = nestedValue(...args);
|
|
147
|
+
return this.wrapSchema(schema, name);
|
|
148
|
+
};
|
|
149
|
+
return nestedValue;
|
|
150
|
+
} });
|
|
151
|
+
return value;
|
|
152
|
+
} });
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Creates a new ConfigParser object that can be used to parse the config object
|
|
156
|
+
*
|
|
157
|
+
* @param builder - A function that takes a getter function and returns a config object
|
|
158
|
+
* @returns A ConfigParser object that can be used to parse the config object
|
|
159
|
+
*/
|
|
160
|
+
create(builder) {
|
|
161
|
+
const config = builder(this.getZodGetter);
|
|
162
|
+
return new ConfigParser(config);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
//#endregion
|
|
167
|
+
Object.defineProperty(exports, 'ConfigParser', {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
get: function () {
|
|
170
|
+
return ConfigParser;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
Object.defineProperty(exports, 'EnvironmentParser', {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
get: function () {
|
|
176
|
+
return EnvironmentParser;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import get from "lodash.get";
|
|
2
|
+
import set from "lodash.set";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
|
|
5
|
+
//#region src/EnvironmentParser.ts
|
|
6
|
+
/**
|
|
7
|
+
* Parses and validates configuration objects against Zod schemas.
|
|
8
|
+
* Handles nested configurations and aggregates validation errors.
|
|
9
|
+
*
|
|
10
|
+
* @template TResponse - The shape of the configuration object
|
|
11
|
+
*/
|
|
12
|
+
var ConfigParser = class {
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new ConfigParser instance.
|
|
15
|
+
*
|
|
16
|
+
* @param config - The configuration object to parse
|
|
17
|
+
*/
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parses the config object and validates it against the Zod schemas
|
|
23
|
+
* @returns The parsed config object
|
|
24
|
+
*/
|
|
25
|
+
parse() {
|
|
26
|
+
const errors = [];
|
|
27
|
+
const parseDeep = (config, path = []) => {
|
|
28
|
+
const result = {};
|
|
29
|
+
if (config && typeof config !== "object") return config;
|
|
30
|
+
for (const key in config) {
|
|
31
|
+
const schema = config[key];
|
|
32
|
+
const currentPath = [...path, key];
|
|
33
|
+
if (schema instanceof z.ZodType) {
|
|
34
|
+
const parsed = schema.safeParse(void 0);
|
|
35
|
+
if (parsed.success) set(result, key, parsed.data);
|
|
36
|
+
else errors.push(...parsed.error.issues.map((issue) => ({
|
|
37
|
+
...issue,
|
|
38
|
+
path: [...currentPath, ...issue.path]
|
|
39
|
+
})));
|
|
40
|
+
} else if (schema) set(result, key, parseDeep(schema, currentPath));
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
const parsedConfig = parseDeep(this.config);
|
|
45
|
+
if (errors.length > 0) throw new z.ZodError(errors);
|
|
46
|
+
return parsedConfig;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Parses environment variables with type-safe validation using Zod schemas.
|
|
51
|
+
* Provides a fluent API for defining environment variable schemas with automatic
|
|
52
|
+
* error context enrichment.
|
|
53
|
+
*
|
|
54
|
+
* @template T - The type of the configuration object (typically process.env)
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const config = new EnvironmentParser(process.env)
|
|
59
|
+
* .create((get) => ({
|
|
60
|
+
* port: get('PORT').string().transform(Number).default(3000),
|
|
61
|
+
* database: {
|
|
62
|
+
* url: get('DATABASE_URL').string().url()
|
|
63
|
+
* }
|
|
64
|
+
* }))
|
|
65
|
+
* .parse();
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
var EnvironmentParser = class {
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new EnvironmentParser instance.
|
|
71
|
+
*
|
|
72
|
+
* @param config - The configuration object to parse (typically process.env)
|
|
73
|
+
*/
|
|
74
|
+
constructor(config) {
|
|
75
|
+
this.config = config;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages
|
|
79
|
+
* with environment variable context.
|
|
80
|
+
*
|
|
81
|
+
* @param schema - The Zod schema to wrap
|
|
82
|
+
* @param name - The environment variable name for error context
|
|
83
|
+
* @returns A wrapped Zod schema with enhanced error reporting
|
|
84
|
+
*/
|
|
85
|
+
wrapSchema = (schema, name) => {
|
|
86
|
+
return new Proxy(schema, { get: (target, prop) => {
|
|
87
|
+
if (prop === "parse") return () => {
|
|
88
|
+
const value = get(this.config, name);
|
|
89
|
+
try {
|
|
90
|
+
return target.parse(value);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error instanceof z.ZodError) {
|
|
93
|
+
const modifiedIssues = error.issues.map((issue) => ({
|
|
94
|
+
...issue,
|
|
95
|
+
message: `Environment variable "${name}": ${issue.message}`,
|
|
96
|
+
path: [name, ...issue.path]
|
|
97
|
+
}));
|
|
98
|
+
throw new z.ZodError(modifiedIssues);
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
if (prop === "safeParse") return () => {
|
|
104
|
+
const value = get(this.config, name);
|
|
105
|
+
const result = target.safeParse(value);
|
|
106
|
+
if (!result.success) {
|
|
107
|
+
const modifiedIssues = result.error.issues.map((issue) => ({
|
|
108
|
+
...issue,
|
|
109
|
+
message: `Environment variable "${name}": ${issue.message}`,
|
|
110
|
+
path: [name, ...issue.path]
|
|
111
|
+
}));
|
|
112
|
+
return {
|
|
113
|
+
success: false,
|
|
114
|
+
error: new z.ZodError(modifiedIssues)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
};
|
|
119
|
+
const originalProp = target[prop];
|
|
120
|
+
if (typeof originalProp === "function") return (...args) => {
|
|
121
|
+
const result = originalProp.apply(target, args);
|
|
122
|
+
if (result && typeof result === "object" && "parse" in result) return this.wrapSchema(result, name);
|
|
123
|
+
return result;
|
|
124
|
+
};
|
|
125
|
+
return originalProp;
|
|
126
|
+
} });
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Creates a proxied version of the Zod object that wraps all schema creators
|
|
130
|
+
* to provide enhanced error messages with environment variable context.
|
|
131
|
+
*
|
|
132
|
+
* @param name - The environment variable name
|
|
133
|
+
* @returns A proxied Zod object with wrapped schema creators
|
|
134
|
+
*/
|
|
135
|
+
getZodGetter = (name) => {
|
|
136
|
+
return new Proxy({ ...z }, { get: (target, prop) => {
|
|
137
|
+
const value = target[prop];
|
|
138
|
+
if (typeof value === "function") return (...args) => {
|
|
139
|
+
const schema = value(...args);
|
|
140
|
+
return this.wrapSchema(schema, name);
|
|
141
|
+
};
|
|
142
|
+
if (value && typeof value === "object") return new Proxy(value, { get: (nestedTarget, nestedProp) => {
|
|
143
|
+
const nestedValue = nestedTarget[nestedProp];
|
|
144
|
+
if (typeof nestedValue === "function") return (...args) => {
|
|
145
|
+
const schema = nestedValue(...args);
|
|
146
|
+
return this.wrapSchema(schema, name);
|
|
147
|
+
};
|
|
148
|
+
return nestedValue;
|
|
149
|
+
} });
|
|
150
|
+
return value;
|
|
151
|
+
} });
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Creates a new ConfigParser object that can be used to parse the config object
|
|
155
|
+
*
|
|
156
|
+
* @param builder - A function that takes a getter function and returns a config object
|
|
157
|
+
* @returns A ConfigParser object that can be used to parse the config object
|
|
158
|
+
*/
|
|
159
|
+
create(builder) {
|
|
160
|
+
const config = builder(this.getZodGetter);
|
|
161
|
+
return new ConfigParser(config);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
export { ConfigParser, EnvironmentParser };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_EnvironmentParser = require('./EnvironmentParser-
|
|
1
|
+
const require_EnvironmentParser = require('./EnvironmentParser-Bo2CCl_K.cjs');
|
|
2
2
|
|
|
3
3
|
exports.ConfigParser = require_EnvironmentParser.ConfigParser;
|
|
4
4
|
exports.EnvironmentParser = require_EnvironmentParser.EnvironmentParser;
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
|
+
const require_EnvironmentParser = require('../EnvironmentParser-Bo2CCl_K.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
|