env-typed-checker 0.2.0 → 0.2.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/README.md CHANGED
@@ -6,19 +6,26 @@ It helps your app fail fast when configuration is wrong:
6
6
 
7
7
  - ❌ Missing required variables
8
8
  - ❌ Wrong types (e.g. `PORT="abc"`)
9
- - ❌ Invalid URLs or JSON
10
- - ❌ Silent mistakes that only crash later
9
+ - ❌ Invalid URLs / emails / JSON
10
+ - ❌ Values not matching allowed enums or regex patterns
11
+ - ✅ Optional values + defaults
12
+ - ✅ CLI checks for CI + .env generation
13
+
11
14
 
12
15
  ---
13
16
 
14
17
  ## ✨ Features
15
18
 
16
- - Simple schema syntax (`"number"`, `"boolean?"`, `"url"`, `"json"`, `"string"`)
17
- - Loads `.env` automatically via `dotenv` (optional)
18
- - Aggregated, friendly error output (shows all issues at once)
19
- - TypeScript types inferred from the schema
20
- - CLI for quick checks (great for CI)
21
- - Minimal dependencies (runtime: `dotenv` only)
19
+ - Simple schema syntax (`"number"`, `"boolean?"`, `"email"` `"url"`, `"json"`, `"string"`)
20
+ - Advanced specs: `enum` and `regex`
21
+ - Optional values with `?` and `optional: true`
22
+ - Defaults (typed + validated)
23
+ - CLI:
24
+
25
+ - `check` → validate env
26
+ - `generate` → generate/update `.env` from schema (no overwrite by default)
27
+ - Uses `.env` via `dotenv` (optional)
28
+ - Friendly aggregated errors (see everything that’s wrong at once)
22
29
 
23
30
  ---
24
31
 
@@ -28,6 +35,12 @@ It helps your app fail fast when configuration is wrong:
28
35
  npm install env-typed-checker
29
36
  ```
30
37
 
38
+ Or run via npx:
39
+
40
+ ```bash
41
+ npx env-typed-checker --help
42
+ ```
43
+
31
44
  ## 🚀 Quick Start (Code)
32
45
 
33
46
  ```ts
@@ -36,8 +49,10 @@ import { envDoctor } from "env-typed-checker";
36
49
  export const config = envDoctor({
37
50
  PORT: "number",
38
51
  DB_URL: "url",
39
- DEBUG: "boolean?"
52
+ ADMIN_EMAIL: "email",
53
+ DEBUG: "boolean?",
40
54
  });
55
+
41
56
  ```
42
57
 
43
58
  ### What you get
@@ -46,6 +61,8 @@ export const config = envDoctor({
46
61
 
47
62
  * DB_URL → string (validated as URL)
48
63
 
64
+ * ADMIN_EMAIL → string (validated as email)
65
+
49
66
  * DEBUG → boolean | undefined (optional)
50
67
 
51
68
 
@@ -57,6 +74,7 @@ export const config = envDoctor({
57
74
  | **boolean** | Supports `true` / `false`, `1` / `0`, and `yes` / `no` |
58
75
  | **json** | Validates and parses a valid JSON string |
59
76
  | **url** | Validates for a properly formatted URL |
77
+ | **email** | Validates for a properly formatted email |
60
78
 
61
79
 
62
80
  ### Optional Values
@@ -64,8 +82,53 @@ Add ? to make a variable optional:
64
82
  ```ts
65
83
  envDoctor({ DEBUG: "boolean?" });
66
84
  ```
67
- If missing → value will be undefined.
68
85
 
86
+ Or use object-style:
87
+
88
+ ```ts
89
+ envDoctor({
90
+ DEBUG: { type: "boolean", optional: true },
91
+ });
92
+ ```
93
+ Missing optional vars become `undefined` unless you provide a default.
94
+
95
+ ### 🎯 Defaults
96
+ Defaults can be provided in object-style specs.
97
+
98
+ ```ts
99
+ import { envDoctor } from "env-typed-checker";
100
+
101
+ const config = envDoctor({
102
+ PORT: { type: "number", default: 3000 },
103
+ DEBUG: { type: "boolean", default: "false" }, // string is allowed (parsed)
104
+ NODE_ENV: { type: "enum", values: ["dev", "prod"], default: "dev" },
105
+ });
106
+ ```
107
+
108
+ ### Notes:
109
+
110
+ - `number` default: number or string (string will be parsed)
111
+ - `boolean` default: boolean or string (string will be parsed)
112
+ - `url/email` defaults must be strings and must validate
113
+ - `json` defaults can be any JSON-like value
114
+
115
+ ### ✅ Enum and Regex
116
+
117
+ ### Enum
118
+
119
+ ```ts
120
+ const config = envDoctor({
121
+ NODE_ENV: { type: "enum", values: ["dev", "prod"] },
122
+ });
123
+ ```
124
+
125
+ ### Regex
126
+
127
+ ```ts
128
+ const config = envDoctor({
129
+ SLUG: { type: "regex", pattern: "^[a-z0-9-]+$", flags: "i" },
130
+ });
131
+ ```
69
132
 
70
133
  ### ⚙️ Options
71
134
 
@@ -76,6 +139,7 @@ envDoctor(schema, {
76
139
  });
77
140
  ```
78
141
 
142
+
79
143
  ### 🧪 Testing with custom env
80
144
 
81
145
  ```ts
@@ -96,6 +160,7 @@ Given a `.env` like:
96
160
  ```env
97
161
  PORT=abc
98
162
  DB_URL=not-a-url
163
+ NODE_ENV=staging
99
164
  ```
100
165
 
101
166
  ```ts
@@ -104,6 +169,7 @@ import { envDoctor } from "env-typed-checker";
104
169
  envDoctor({
105
170
  PORT: "number",
106
171
  DB_URL: "url"
172
+ NODE_ENV: { type: "enum", values: ["dev", "prod"] },
107
173
  });
108
174
  ```
109
175
 
@@ -113,6 +179,7 @@ envDoctor({
113
179
  ENV validation failed
114
180
  - PORT: expected number, got "abc"
115
181
  - DB_URL: expected url, got "not-a-url"
182
+ - NODE_ENV: must be one of [dev, prod]
116
183
  ```
117
184
  All errors are shown together so you can fix them in one go.
118
185
 
@@ -127,7 +194,10 @@ Validate your environment without writing code — perfect for CI pipelines.
127
194
  {
128
195
  "PORT": "number",
129
196
  "DB_URL": "url",
130
- "DEBUG": "boolean?"
197
+ "ADMIN_EMAIL": "email",
198
+ "DEBUG": "boolean?",
199
+ "NODE_ENV": { "type": "enum", "values": ["dev", "prod"] },
200
+ "SLUG": { "type": "regex", "pattern": "^[a-z0-9-]+$", "flags": "i" }
131
201
  }
132
202
  ```
133
203
 
@@ -137,17 +207,48 @@ Validate your environment without writing code — perfect for CI pipelines.
137
207
  npx env-typed-checker check --schema env.schema.json
138
208
  ```
139
209
 
140
- ### Options
210
+ Useful flags
141
211
 
142
212
  ```bash
143
- # Custom env file
213
+ # Use a specific env file (instead of .env)
144
214
  npx env-typed-checker check --schema env.schema.json --env-file .env.production
145
215
 
146
- # Skip dotenv (use process.env only)
216
+ # Skip dotenv loading and validate only process.env
147
217
  npx env-typed-checker check --schema env.schema.json --no-dotenv
148
218
  ```
149
219
 
150
- ## Exit codes
220
+ ### `generate` — generate or update `.env`
221
+
222
+ Generate values from schema (writes missing keys; does not overwrite existing values).
223
+
224
+ ```bash
225
+ npx env-typed-checker generate --schema env.schema.json
226
+ ```
227
+ ### By default:
228
+
229
+ - output file: .env
230
+ - mode: update
231
+
232
+ ### Flags
233
+
234
+ ```bash
235
+ # Custom output file
236
+ npx env-typed-checker generate --schema env.schema.json --out .env.example
237
+
238
+ # Create mode: refuse to overwrite an existing file
239
+ npx env-typed-checker generate --schema env.schema.json --out .env --mode=create
240
+
241
+ # Update mode: append only missing keys (safe)
242
+ npx env-typed-checker generate --schema env.schema.json --out .env --mode=update
243
+
244
+ # Do not write defaults (leave blank values)
245
+ npx env-typed-checker generate --schema env.schema.json --no-defaults
246
+
247
+ # Add inline type comments (useful for .env.example)
248
+ npx env-typed-checker generate --schema env.schema.json --comment-types
249
+ ```
250
+
251
+ ### Exit codes
151
252
 
152
253
  * `0` = OK
153
254
 
@@ -163,14 +264,20 @@ Add this to your workflow to fail the build if env is invalid:
163
264
  - name: Validate env
164
265
  run: npx env-typed-checker check --schema env.schema.json
165
266
  ```
166
- (If you use a different env file in CI, pass --env-file.)
267
+ If you use a specific env file in CI:
268
+
269
+ ```yml
270
+ - name: Validate env
271
+ run: npx env-typed-checker check --schema env.schema.json --env-file .env.ci
272
+ ```
167
273
 
168
274
  ## 🛠 Development
169
275
  Clone the repo and install:
170
276
  ```bash
171
277
  npm install
278
+ npm test
172
279
  ```
173
- ### Available Scripts
280
+ ### Common scripts:
174
281
  ```bash
175
282
  npm run build # build package
176
283
  npm run test # run tests
@@ -181,24 +288,10 @@ npm run dev # watch build
181
288
  ### 🤝 Contributing
182
289
  PRs are welcome!
183
290
 
184
- * Add new validators (e.g. `enum`, `regex`, `email`)
185
-
291
+ * Improve docs / examples
292
+ * Add more schema features
186
293
  * Improve CLI output formatting
187
-
188
- * Add .env.example generator
189
-
190
- * Improve docs & examples
191
-
192
- * Please read CONTRIBUTING.md before opening a PR.
193
-
194
- ### 📌 Roadmap
195
- * `.env.example` generator
196
-
197
- * Strict mode: warn on unknown variables
198
-
199
- * More schema features: enums, defaults, min/max
200
-
201
- * Framework helpers (Next.js / Express / etc.)
294
+ * Add integrations / templates
202
295
 
203
296
 
204
297
  # 📝 License
@@ -0,0 +1,262 @@
1
+ // src/core/envDoctor.ts
2
+ import * as dotenv from "dotenv";
3
+
4
+ // src/errors/EnvDoctorError.ts
5
+ var EnvDoctorError = class extends Error {
6
+ constructor(issues) {
7
+ const header = "ENV validation failed";
8
+ const lines = issues.map((i) => `- ${i.key}: ${i.message}`);
9
+ super([header, ...lines].join("\n"));
10
+ this.name = "EnvDoctorError";
11
+ this.issues = issues;
12
+ }
13
+ };
14
+
15
+ // src/validators/primitives.ts
16
+ var EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
17
+ function parseByType(type, raw) {
18
+ switch (type) {
19
+ case "string":
20
+ return raw;
21
+ case "number": {
22
+ const n = Number(raw.trim());
23
+ if (!Number.isFinite(n)) throw new Error(`expected number, got "${raw}"`);
24
+ return n;
25
+ }
26
+ case "boolean": {
27
+ const v = raw.trim().toLowerCase();
28
+ if (["true", "1", "yes", "y", "on"].includes(v)) return true;
29
+ if (["false", "0", "no", "n", "off"].includes(v)) return false;
30
+ throw new Error(
31
+ `expected boolean (true/false/1/0/yes/no/on/off), got "${raw}"`
32
+ );
33
+ }
34
+ case "json": {
35
+ try {
36
+ return JSON.parse(raw);
37
+ } catch {
38
+ throw new Error(`expected json, got "${raw}"`);
39
+ }
40
+ }
41
+ case "url": {
42
+ try {
43
+ new URL(raw);
44
+ return raw;
45
+ } catch {
46
+ throw new Error(`expected url, got "${raw}"`);
47
+ }
48
+ }
49
+ case "email": {
50
+ const s = raw.trim();
51
+ if (!EMAIL_RE.test(s)) {
52
+ throw new Error(`expected email, got "${raw}"`);
53
+ }
54
+ return s;
55
+ }
56
+ default: {
57
+ throw new Error(`unsupported primitive type: ${String(type)}`);
58
+ }
59
+ }
60
+ }
61
+ function hasOwn(obj, key) {
62
+ return Object.prototype.hasOwnProperty.call(obj, key);
63
+ }
64
+ function coerceDefault(kind, def) {
65
+ if (def === void 0) return void 0;
66
+ switch (kind) {
67
+ case "string": {
68
+ if (typeof def !== "string")
69
+ throw new Error(`default for string must be a string`);
70
+ return def;
71
+ }
72
+ case "number": {
73
+ if (typeof def === "number") {
74
+ if (!Number.isFinite(def))
75
+ throw new Error(`default for number must be finite`);
76
+ return def;
77
+ }
78
+ if (typeof def === "string") return parseByType("number", def);
79
+ throw new Error(`default for number must be number or string`);
80
+ }
81
+ case "boolean": {
82
+ if (typeof def === "boolean") return def;
83
+ if (typeof def === "string") return parseByType("boolean", def);
84
+ throw new Error(`default for boolean must be boolean or string`);
85
+ }
86
+ case "json": {
87
+ return def;
88
+ }
89
+ case "url": {
90
+ if (typeof def !== "string")
91
+ throw new Error(`default for url must be a string`);
92
+ return parseByType("url", def);
93
+ }
94
+ case "email": {
95
+ if (typeof def !== "string")
96
+ throw new Error(`default for email must be a string`);
97
+ return parseByType("email", def);
98
+ }
99
+ /* c8 ignore next */
100
+ default: {
101
+ throw new Error(`unsupported default kind: ${String(kind)}`);
102
+ }
103
+ }
104
+ }
105
+ function normalizeSpec(schemaValue) {
106
+ if (typeof schemaValue === "string") {
107
+ const optional = schemaValue.endsWith("?");
108
+ const base = optional ? schemaValue.slice(0, -1) : schemaValue;
109
+ const allowed = [
110
+ "string",
111
+ "number",
112
+ "boolean",
113
+ "json",
114
+ "url",
115
+ "email"
116
+ ];
117
+ if (!allowed.includes(base)) {
118
+ throw new Error(
119
+ `Unsupported type "${schemaValue}". Supported: string, number, boolean, json, url, email (optional with ?)`
120
+ );
121
+ }
122
+ return { kind: base, optional };
123
+ }
124
+ if (!schemaValue || typeof schemaValue !== "object" || Array.isArray(schemaValue)) {
125
+ throw new Error("Schema value must be a string or object spec.");
126
+ }
127
+ const t = schemaValue.type;
128
+ const primitiveAllowed = [
129
+ "string",
130
+ "number",
131
+ "boolean",
132
+ "json",
133
+ "url",
134
+ "email"
135
+ ];
136
+ if (primitiveAllowed.includes(t)) {
137
+ const optional = !!schemaValue.optional;
138
+ const defaultValue = hasOwn(schemaValue, "default") ? coerceDefault(t, schemaValue.default) : void 0;
139
+ return { kind: t, optional, defaultValue };
140
+ }
141
+ if (t === "enum") {
142
+ const values = schemaValue.values;
143
+ if (!Array.isArray(values) || values.length === 0 || !values.every((v) => typeof v === "string")) {
144
+ throw new Error(`enum spec requires "values": string[] (non-empty)`);
145
+ }
146
+ const optional = !!schemaValue.optional;
147
+ let defaultValue = void 0;
148
+ if (hasOwn(schemaValue, "default")) {
149
+ const def = schemaValue.default;
150
+ if (typeof def !== "string") {
151
+ throw new Error(`default for enum must be a string`);
152
+ }
153
+ if (!values.includes(def)) {
154
+ throw new Error(
155
+ `default "${def}" must be one of [${values.join(", ")}]`
156
+ );
157
+ }
158
+ defaultValue = def;
159
+ }
160
+ return { kind: "enum", optional, values, defaultValue };
161
+ }
162
+ if (t === "regex") {
163
+ const pattern = schemaValue.pattern;
164
+ const flags = schemaValue.flags;
165
+ if (typeof pattern !== "string" || pattern.length === 0) {
166
+ throw new Error(`regex spec requires "pattern": string`);
167
+ }
168
+ if (flags !== void 0 && typeof flags !== "string") {
169
+ throw new Error(`regex spec "flags" must be a string if provided`);
170
+ }
171
+ let re;
172
+ try {
173
+ re = new RegExp(pattern, flags);
174
+ } catch (e) {
175
+ throw new Error(`invalid regex: ${String(e)}`);
176
+ }
177
+ const display = `/${pattern}/${flags ?? ""}`;
178
+ const optional = !!schemaValue.optional;
179
+ let defaultValue = void 0;
180
+ if (hasOwn(schemaValue, "default")) {
181
+ const def = schemaValue.default;
182
+ if (typeof def !== "string") {
183
+ throw new Error(`default for regex must be a string`);
184
+ }
185
+ if (!re.test(def)) {
186
+ throw new Error(`default "${def}" does not match ${display}`);
187
+ }
188
+ defaultValue = def;
189
+ }
190
+ return { kind: "regex", optional, re, display, defaultValue };
191
+ }
192
+ throw new Error(
193
+ `Unsupported object spec type "${String(t)}". Supported: primitives (string/number/boolean/json/url/email), enum, regex`
194
+ );
195
+ }
196
+
197
+ // src/core/runner.ts
198
+ function validateAndParse(schema, env) {
199
+ const issues = [];
200
+ const out = {};
201
+ for (const [key, schemaValue] of Object.entries(schema)) {
202
+ let spec;
203
+ try {
204
+ spec = normalizeSpec(schemaValue);
205
+ } catch (e) {
206
+ const msg = e instanceof Error ? e.message : String(e);
207
+ issues.push({ key, kind: "invalid", message: msg });
208
+ continue;
209
+ }
210
+ const raw = env[key];
211
+ if (raw === void 0 || raw === "") {
212
+ if (spec.defaultValue !== void 0) {
213
+ out[key] = spec.defaultValue;
214
+ } else if (spec.optional) {
215
+ out[key] = void 0;
216
+ } else {
217
+ issues.push({
218
+ key,
219
+ kind: "missing",
220
+ message: "missing required environment variable"
221
+ });
222
+ }
223
+ continue;
224
+ }
225
+ try {
226
+ if (spec.kind === "enum") {
227
+ if (!spec.values.includes(raw)) {
228
+ throw new Error(
229
+ `expected one of [${spec.values.join(", ")}], got "${raw}"`
230
+ );
231
+ }
232
+ out[key] = raw;
233
+ } else if (spec.kind === "regex") {
234
+ if (!spec.re.test(raw)) {
235
+ throw new Error(`does not match ${spec.display}`);
236
+ }
237
+ out[key] = raw;
238
+ } else {
239
+ out[key] = parseByType(spec.kind, raw);
240
+ }
241
+ } catch (e) {
242
+ const msg = e instanceof Error ? e.message : String(e);
243
+ issues.push({ key, kind: "invalid", message: msg });
244
+ }
245
+ }
246
+ if (issues.length > 0) throw new EnvDoctorError(issues);
247
+ return out;
248
+ }
249
+
250
+ // src/core/envDoctor.ts
251
+ function envDoctor(schema, options = {}) {
252
+ const { loadDotEnv = true, env = process.env } = options;
253
+ if (loadDotEnv) dotenv.config();
254
+ return validateAndParse(schema, env);
255
+ }
256
+
257
+ export {
258
+ EnvDoctorError,
259
+ normalizeSpec,
260
+ envDoctor
261
+ };
262
+ //# sourceMappingURL=chunk-L5DK6LRX.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/envDoctor.ts","../src/errors/EnvDoctorError.ts","../src/validators/primitives.ts","../src/core/runner.ts"],"sourcesContent":["import * as dotenv from \"dotenv\";\nimport { validateAndParse } from \"./runner\";\nimport type { EnvDoctorOptions, EnvDoctorResult, EnvDoctorSchema } from \"../types\";\n\nexport type { EnvDoctorOptions, EnvDoctorSchema } from \"../types\";\nexport { EnvDoctorError } from \"../errors/EnvDoctorError\";\n\nexport function envDoctor<TSchema extends EnvDoctorSchema>(\n schema: TSchema,\n options: EnvDoctorOptions = {}\n): EnvDoctorResult<TSchema> {\n const { loadDotEnv = true, env = process.env } = options;\n\n if (loadDotEnv) dotenv.config();\n\n return validateAndParse(schema, env);\n}\n","export type EnvDoctorIssue =\n | { key: string; kind: \"missing\"; message: string }\n | { key: string; kind: \"invalid\"; message: string };\n\nexport class EnvDoctorError extends Error {\n public readonly issues: EnvDoctorIssue[];\n\n constructor(issues: EnvDoctorIssue[]) {\n const header = \"ENV validation failed\";\n const lines = issues.map((i) => `- ${i.key}: ${i.message}`);\n super([header, ...lines].join(\"\\n\"));\n this.name = \"EnvDoctorError\";\n this.issues = issues;\n }\n}\n","import type { EnvBaseType, EnvSchemaValue } from \"../types\";\n\nconst EMAIL_RE = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n\nexport function parseByType(type: EnvBaseType, raw: string): unknown {\n switch (type) {\n case \"string\":\n return raw;\n\n case \"number\": {\n const n = Number(raw.trim());\n if (!Number.isFinite(n)) throw new Error(`expected number, got \"${raw}\"`);\n return n;\n }\n\n case \"boolean\": {\n const v = raw.trim().toLowerCase();\n if ([\"true\", \"1\", \"yes\", \"y\", \"on\"].includes(v)) return true;\n if ([\"false\", \"0\", \"no\", \"n\", \"off\"].includes(v)) return false;\n throw new Error(\n `expected boolean (true/false/1/0/yes/no/on/off), got \"${raw}\"`,\n );\n }\n\n case \"json\": {\n try {\n return JSON.parse(raw);\n } catch {\n throw new Error(`expected json, got \"${raw}\"`);\n }\n }\n\n case \"url\": {\n try {\n new URL(raw);\n return raw;\n } catch {\n throw new Error(`expected url, got \"${raw}\"`);\n }\n }\n\n case \"email\": {\n const s = raw.trim();\n if (!EMAIL_RE.test(s)) {\n throw new Error(`expected email, got \"${raw}\"`);\n }\n return s;\n }\n\n default: {\n throw new Error(`unsupported primitive type: ${String(type)}`);\n }\n }\n}\n\nfunction hasOwn(obj: unknown, key: string): boolean {\n return Object.prototype.hasOwnProperty.call(obj, key);\n}\n\n/**\n * Type-check + normalize a default value into the right runtime type.\n * - Allows string defaults for number/boolean (parsed)\n * - Validates url/email defaults\n * - json defaults can be any value\n */\nfunction coerceDefault(kind: EnvBaseType, def: unknown): unknown {\n if (def === undefined) return undefined;\n\n switch (kind) {\n case \"string\": {\n if (typeof def !== \"string\")\n throw new Error(`default for string must be a string`);\n return def;\n }\n\n case \"number\": {\n if (typeof def === \"number\") {\n if (!Number.isFinite(def))\n throw new Error(`default for number must be finite`);\n return def;\n }\n if (typeof def === \"string\") return parseByType(\"number\", def);\n throw new Error(`default for number must be number or string`);\n }\n\n case \"boolean\": {\n if (typeof def === \"boolean\") return def;\n if (typeof def === \"string\") return parseByType(\"boolean\", def);\n throw new Error(`default for boolean must be boolean or string`);\n }\n\n case \"json\": {\n // allow any JSON-ish value\n return def;\n }\n\n case \"url\": {\n if (typeof def !== \"string\")\n throw new Error(`default for url must be a string`);\n return parseByType(\"url\", def);\n }\n\n case \"email\": {\n if (typeof def !== \"string\")\n throw new Error(`default for email must be a string`);\n return parseByType(\"email\", def);\n }\n\n /* c8 ignore next */\n default: {\n throw new Error(`unsupported default kind: ${String(kind)}`);\n }\n }\n}\n\n/** Normalized spec used by the runner */\nexport type NormalizedSpec =\n | { kind: EnvBaseType; optional: boolean; defaultValue?: unknown }\n | {\n kind: \"enum\";\n optional: boolean;\n values: readonly string[];\n defaultValue?: string;\n }\n | {\n kind: \"regex\";\n optional: boolean;\n re: RegExp;\n display: string;\n defaultValue?: string;\n };\n\nexport function normalizeSpec(schemaValue: EnvSchemaValue): NormalizedSpec {\n // --------------------\n // String style: \"number?\" etc.\n // --------------------\n if (typeof schemaValue === \"string\") {\n const optional = schemaValue.endsWith(\"?\");\n const base = optional ? schemaValue.slice(0, -1) : schemaValue;\n\n const allowed: readonly EnvBaseType[] = [\n \"string\",\n \"number\",\n \"boolean\",\n \"json\",\n \"url\",\n \"email\",\n ];\n\n if (!allowed.includes(base as EnvBaseType)) {\n throw new Error(\n `Unsupported type \"${schemaValue}\". Supported: string, number, boolean, json, url, email (optional with ?)`,\n );\n }\n\n return { kind: base as EnvBaseType, optional };\n }\n\n // --------------------\n // Object style\n // --------------------\n if (\n !schemaValue ||\n typeof schemaValue !== \"object\" ||\n Array.isArray(schemaValue)\n ) {\n throw new Error(\"Schema value must be a string or object spec.\");\n }\n\n const t = (schemaValue as any).type;\n\n // --------------------\n // Primitive object spec: { type: \"number\", optional?: true, default?: ... }\n // --------------------\n const primitiveAllowed: readonly EnvBaseType[] = [\n \"string\",\n \"number\",\n \"boolean\",\n \"json\",\n \"url\",\n \"email\",\n ];\n\n if (primitiveAllowed.includes(t)) {\n const optional = !!(schemaValue as any).optional;\n const defaultValue = hasOwn(schemaValue, \"default\")\n ? coerceDefault(t as EnvBaseType, (schemaValue as any).default)\n : undefined;\n\n return { kind: t as EnvBaseType, optional, defaultValue };\n }\n\n // --------------------\n // Enum: { type: \"enum\", values: [...], optional?: true, default?: \"dev\" }\n // --------------------\n if (t === \"enum\") {\n const values = (schemaValue as any).values;\n if (\n !Array.isArray(values) ||\n values.length === 0 ||\n !values.every((v: any) => typeof v === \"string\")\n ) {\n throw new Error(`enum spec requires \"values\": string[] (non-empty)`);\n }\n\n const optional = !!(schemaValue as any).optional;\n\n let defaultValue: string | undefined = undefined;\n if (hasOwn(schemaValue, \"default\")) {\n const def = (schemaValue as any).default;\n if (typeof def !== \"string\") {\n throw new Error(`default for enum must be a string`);\n }\n if (!values.includes(def)) {\n throw new Error(\n `default \"${def}\" must be one of [${values.join(\", \")}]`,\n );\n }\n defaultValue = def;\n }\n\n return { kind: \"enum\", optional, values, defaultValue };\n }\n\n // --------------------\n // Regex: { type: \"regex\", pattern: \"...\", flags?: \"...\", optional?: true, default?: \"abc\" }\n // --------------------\n if (t === \"regex\") {\n const pattern = (schemaValue as any).pattern;\n const flags = (schemaValue as any).flags;\n\n if (typeof pattern !== \"string\" || pattern.length === 0) {\n throw new Error(`regex spec requires \"pattern\": string`);\n }\n if (flags !== undefined && typeof flags !== \"string\") {\n throw new Error(`regex spec \"flags\" must be a string if provided`);\n }\n\n let re: RegExp;\n try {\n re = new RegExp(pattern, flags);\n } catch (e) {\n throw new Error(`invalid regex: ${String(e)}`);\n }\n\n const display = `/${pattern}/${flags ?? \"\"}`;\n const optional = !!(schemaValue as any).optional;\n\n let defaultValue: string | undefined = undefined;\n if (hasOwn(schemaValue, \"default\")) {\n const def = (schemaValue as any).default;\n if (typeof def !== \"string\") {\n throw new Error(`default for regex must be a string`);\n }\n if (!re.test(def)) {\n throw new Error(`default \"${def}\" does not match ${display}`);\n }\n defaultValue = def;\n }\n\n return { kind: \"regex\", optional, re, display, defaultValue };\n }\n\n throw new Error(\n `Unsupported object spec type \"${String(t)}\". Supported: primitives (string/number/boolean/json/url/email), enum, regex`,\n );\n}\n","import { EnvDoctorError, type EnvDoctorIssue } from \"../errors/EnvDoctorError\";\nimport { normalizeSpec, parseByType } from \"../validators/primitives\";\nimport type { EnvDoctorResult, EnvDoctorSchema } from \"../types\";\n\nexport function validateAndParse<TSchema extends EnvDoctorSchema>(\n schema: TSchema,\n env: Record<string, string | undefined>,\n): EnvDoctorResult<TSchema> {\n const issues: EnvDoctorIssue[] = [];\n const out: Record<string, unknown> = {};\n\n for (const [key, schemaValue] of Object.entries(schema)) {\n let spec: ReturnType<typeof normalizeSpec>;\n\n try {\n spec = normalizeSpec(schemaValue);\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n issues.push({ key, kind: \"invalid\", message: msg });\n continue;\n }\n\n const raw = env[key];\n\n // treat undefined or empty string as \"missing\"\n if (raw === undefined || raw === \"\") {\n if (spec.defaultValue !== undefined) {\n out[key] = spec.defaultValue;\n } else if (spec.optional) {\n out[key] = undefined;\n } else {\n issues.push({\n key,\n kind: \"missing\",\n message: \"missing required environment variable\",\n });\n }\n continue;\n }\n\n try {\n if (spec.kind === \"enum\") {\n if (!spec.values.includes(raw)) {\n throw new Error(\n `expected one of [${spec.values.join(\", \")}], got \"${raw}\"`,\n );\n }\n out[key] = raw;\n } else if (spec.kind === \"regex\") {\n if (!spec.re.test(raw)) {\n throw new Error(`does not match ${spec.display}`);\n }\n out[key] = raw;\n } else {\n // primitives: string/number/boolean/json/url/email\n out[key] = parseByType(spec.kind, raw);\n }\n } catch (e) {\n const msg = e instanceof Error ? e.message : String(e);\n issues.push({ key, kind: \"invalid\", message: msg });\n }\n }\n\n if (issues.length > 0) throw new EnvDoctorError(issues);\n\n return out as EnvDoctorResult<TSchema>;\n}\n"],"mappings":";AAAA,YAAY,YAAY;;;ACIjB,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAGxC,YAAY,QAA0B;AACpC,UAAM,SAAS;AACf,UAAM,QAAQ,OAAO,IAAI,CAAC,MAAM,KAAK,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE;AAC1D,UAAM,CAAC,QAAQ,GAAG,KAAK,EAAE,KAAK,IAAI,CAAC;AACnC,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;;;ACZA,IAAM,WAAW;AAEV,SAAS,YAAY,MAAmB,KAAsB;AACnE,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,aAAO;AAAA,IAET,KAAK,UAAU;AACb,YAAM,IAAI,OAAO,IAAI,KAAK,CAAC;AAC3B,UAAI,CAAC,OAAO,SAAS,CAAC,EAAG,OAAM,IAAI,MAAM,yBAAyB,GAAG,GAAG;AACxE,aAAO;AAAA,IACT;AAAA,IAEA,KAAK,WAAW;AAC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