@safe-hand/safe-env-check 1.1.1 → 1.1.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/README.md +284 -36
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +668 -0
- package/dist/index.d.ts +28 -18
- package/dist/index.js +517 -34
- package/package.json +7 -6
- package/.github/workflows/release.yml +0 -36
- package/CHANGELOG.md +0 -39
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -170
- package/jest.config.js +0 -5
- package/src/cli.ts +0 -143
- package/src/index.ts +0 -3
- package/src/types.ts +0 -17
- package/src/validateEnv.ts +0 -74
- package/tests/cli.test.ts +0 -200
- package/tests/validateEnv.test.ts +0 -63
- package/tsconfig.json +0 -15
package/README.md
CHANGED
|
@@ -4,15 +4,37 @@
|
|
|
4
4
|

|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
A tiny TypeScript
|
|
7
|
+
A tiny TypeScript-first environment variable validator that ensures your application starts with a correct configuration.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
It validates process.env using a schema and provides strong **TypeScript typing**, **CLI validation**, and **CI/CD integration**.
|
|
10
|
+
|
|
11
|
+
## Why safe-env-check?
|
|
12
|
+
|
|
13
|
+
Environment variables are string-based and error-prone.
|
|
14
|
+
|
|
15
|
+
Common problems:
|
|
16
|
+
|
|
17
|
+
- Missing required variables
|
|
18
|
+
- Incorrect types
|
|
19
|
+
- Unexpected environment variables
|
|
20
|
+
- Misconfigured deployments
|
|
21
|
+
- Incorrect .env files in CI/CD
|
|
22
|
+
|
|
23
|
+
safe-env-check prevents these issues **before your app starts**.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- ✅ Type validation (`string`, `number`, `boolean`, `enum`)
|
|
28
|
+
- ✅ Required values
|
|
29
|
+
- ✅ Default values
|
|
30
|
+
- ✅ Enum validation
|
|
31
|
+
- ✅ Strict mode (no unknown variables)
|
|
32
|
+
- ✅ Prefix support (multi-environment setup)
|
|
13
33
|
- ✅ dotenv integration
|
|
14
34
|
- ✅ Custom error formatting
|
|
35
|
+
- ✅ Type-safe output for TypeScript
|
|
15
36
|
- ✅ CLI support
|
|
37
|
+
- ✅ CI/CD friendly
|
|
16
38
|
|
|
17
39
|
---
|
|
18
40
|
|
|
@@ -28,20 +50,6 @@ or
|
|
|
28
50
|
yarn add @safe-hand/safe-env-check
|
|
29
51
|
```
|
|
30
52
|
|
|
31
|
-
## Features
|
|
32
|
-
|
|
33
|
-
- Validate process.env using a schema
|
|
34
|
-
|
|
35
|
-
- Strongly typed output (TypeScript)
|
|
36
|
-
|
|
37
|
-
- Prevents app startup with invalid configuration
|
|
38
|
-
|
|
39
|
-
- Supports CLI for CI/CD and deployment checks
|
|
40
|
-
|
|
41
|
-
- Customizable error messages
|
|
42
|
-
|
|
43
|
-
- Optional strict mode to disallow unknown variables
|
|
44
|
-
|
|
45
53
|
## Basic Usage
|
|
46
54
|
|
|
47
55
|
### Define a schema
|
|
@@ -69,9 +77,9 @@ console.log(env.PORT); // number
|
|
|
69
77
|
console.log(env.NODE_ENV); // "development" | "production"
|
|
70
78
|
```
|
|
71
79
|
|
|
72
|
-
|
|
80
|
+
### Schema Options
|
|
73
81
|
|
|
74
|
-
Each environment
|
|
82
|
+
Each environment variable supports the following options:
|
|
75
83
|
|
|
76
84
|
| Field | Type | Description |
|
|
77
85
|
| ---------- | ---------------------------------------------- | -------------------------------- |
|
|
@@ -80,25 +88,162 @@ Each environment variables supports the following options:
|
|
|
80
88
|
| `default` | `any` | Default value if not provided |
|
|
81
89
|
| `values` | `string[]` | Required for `enum` type |
|
|
82
90
|
|
|
83
|
-
|
|
91
|
+
### Example Schema
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const schema = {
|
|
95
|
+
DATABASE_URL: { type: "string", required: true },
|
|
96
|
+
PORT: { type: "number", default: 3000 },
|
|
97
|
+
DEBUG: { type: "boolean", default: false },
|
|
98
|
+
MODE: {
|
|
99
|
+
type: "enum",
|
|
100
|
+
values: ["dev", "prod"],
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Default Values
|
|
106
|
+
|
|
107
|
+
If a variable is missing, the default value will be used.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
const schema = {
|
|
111
|
+
PORT: { type: "number", default: 3000 },
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**`.env`**
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# PORT missing
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Result:**
|
|
122
|
+
|
|
123
|
+
```cmd
|
|
124
|
+
PORT = 3000
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Required Variables
|
|
84
128
|
|
|
85
129
|
```ts
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
130
|
+
const schema = {
|
|
131
|
+
DATABASE_URL: { type: "string", required: true },
|
|
132
|
+
};
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
If missing:
|
|
136
|
+
|
|
137
|
+
```cmd
|
|
138
|
+
Error ❌ Environment validation failed:
|
|
139
|
+
- DATABASE_URL is required
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Boolean Parsing
|
|
143
|
+
|
|
144
|
+
Boolean values accept:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
true
|
|
148
|
+
false
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Example:**
|
|
152
|
+
|
|
153
|
+
```cmd
|
|
154
|
+
DEBUG=true
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Schema**
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
const schema = {
|
|
161
|
+
DEBUG: {
|
|
162
|
+
type: "boolean",
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Enum Validation
|
|
168
|
+
|
|
169
|
+
Restrict values to a predefined list.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
NODE_ENV: {
|
|
173
|
+
type: "enum",
|
|
174
|
+
values: ["development", "production", "test"],
|
|
175
|
+
}
|
|
89
176
|
```
|
|
90
177
|
|
|
91
|
-
|
|
178
|
+
Invalid values will throw an error.
|
|
179
|
+
|
|
180
|
+
### Strict Mode
|
|
92
181
|
|
|
93
|
-
|
|
182
|
+
Prevent unexpected environment variables.
|
|
94
183
|
|
|
95
184
|
```ts
|
|
96
185
|
validateEnv(schema, { strict: true });
|
|
97
186
|
```
|
|
98
187
|
|
|
99
|
-
If
|
|
188
|
+
If .env contains variables not defined in the schema:
|
|
189
|
+
|
|
190
|
+
```cmd
|
|
191
|
+
Unknown env variables: SOME_RANDOM_VAR
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Prefix Support
|
|
195
|
+
|
|
196
|
+
Useful for multi-service or multi-tenant configs.
|
|
197
|
+
|
|
198
|
+
**Example** `.env`:
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
API_PORT=3000
|
|
202
|
+
API_SECRET=123
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Schema:**
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
const schema = {
|
|
209
|
+
PORT: { type: "number" },
|
|
210
|
+
SECRET: { type: "string" },
|
|
211
|
+
};
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
**Usage:**
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
validateEnv(schema, { prefix: "API_" });
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Env File Support
|
|
221
|
+
|
|
222
|
+
Useful for defining custom env file.
|
|
223
|
+
|
|
224
|
+
**Example** `.env.production`:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
API_PORT=3000
|
|
228
|
+
API_SECRET=123
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
**Schema:**
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const schema = {
|
|
235
|
+
PORT: { type: "number" },
|
|
236
|
+
SECRET: { type: "string" },
|
|
237
|
+
};
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
**Usage:**
|
|
100
241
|
|
|
101
|
-
|
|
242
|
+
```ts
|
|
243
|
+
validateEnv(schema, { prefix: "API_", envFile: ".env.production" });
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Custom Error Formatter
|
|
102
247
|
|
|
103
248
|
You can control how errors are displayed:
|
|
104
249
|
|
|
@@ -108,11 +253,17 @@ validateEnv(schema, {
|
|
|
108
253
|
});
|
|
109
254
|
```
|
|
110
255
|
|
|
111
|
-
##
|
|
256
|
+
## Loading Environment Variables
|
|
257
|
+
|
|
258
|
+
`safe-env-check` uses dotenv to lead the `.env` files\*\*.
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
import { validateEnv } from "@safe-hand/safe-env-check";
|
|
112
262
|
|
|
113
|
-
|
|
263
|
+
const env = validateEnv(schema);
|
|
264
|
+
```
|
|
114
265
|
|
|
115
|
-
Example .
|
|
266
|
+
**Example** `.env.test`:
|
|
116
267
|
|
|
117
268
|
```bash
|
|
118
269
|
PORT=3000
|
|
@@ -120,9 +271,17 @@ JWT_SECRET=supersecret
|
|
|
120
271
|
NODE_ENV=development
|
|
121
272
|
```
|
|
122
273
|
|
|
274
|
+
If you are using the CLI, you can provide a custom environment file:
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
safe-env-check --env-file .env.test
|
|
278
|
+
```
|
|
279
|
+
|
|
123
280
|
## CLI Usage
|
|
124
281
|
|
|
125
|
-
|
|
282
|
+
You can validate environment variables without writing code.
|
|
283
|
+
|
|
284
|
+
This is useful for CI/CD pipelines.
|
|
126
285
|
|
|
127
286
|
```ts
|
|
128
287
|
module.exports = {
|
|
@@ -131,16 +290,105 @@ module.exports = {
|
|
|
131
290
|
};
|
|
132
291
|
```
|
|
133
292
|
|
|
134
|
-
Run validation
|
|
293
|
+
**Run validation:**
|
|
135
294
|
|
|
136
|
-
```
|
|
295
|
+
```cmd
|
|
137
296
|
npx safe-env-check env.schema.js
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
or
|
|
300
|
+
|
|
301
|
+
```cmd
|
|
302
|
+
npx safe-env-check --schema env.schema.js
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### CLI Options
|
|
306
|
+
|
|
307
|
+
| Flag | Description |
|
|
308
|
+
| --------------- | ---------------------------------------------------------------------------------------------- |
|
|
309
|
+
| `--schema` | schema file path |
|
|
310
|
+
| `--env-file` | load a custom env file |
|
|
311
|
+
| `--strict` | enable strict mode |
|
|
312
|
+
| `--prefix` | env variable prefix |
|
|
313
|
+
| `--format json` | output errors in JSON |
|
|
314
|
+
| `--quiet` | Suppress the success message "✅ Environment variables are valid" (errors still exit non-zero) |
|
|
315
|
+
|
|
316
|
+
|
|
|
317
|
+
|
|
318
|
+
### CLI Examples
|
|
319
|
+
|
|
320
|
+
**Basic validation**
|
|
321
|
+
|
|
322
|
+
```cmd
|
|
138
323
|
npx safe-env-check env.schema.js
|
|
139
|
-
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**Using strict mode**
|
|
327
|
+
|
|
328
|
+
```cmd
|
|
329
|
+
npx safe-env-check env.schema.js --strict
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
**Using prefix**
|
|
333
|
+
|
|
334
|
+
```cmd
|
|
335
|
+
npx safe-env-check env.schema.js --prefix API_
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
**Suppress success message**
|
|
339
|
+
|
|
340
|
+
```cmd
|
|
341
|
+
safe-env-check env.schema.js --quiet
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
**Validate production env file**
|
|
345
|
+
|
|
346
|
+
```cmd
|
|
140
347
|
npx safe-env-check env.schema.js --env-file .env.production
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
**JSON error output**
|
|
351
|
+
|
|
352
|
+
```cmd
|
|
141
353
|
npx safe-env-check env.schema.js --format json
|
|
142
354
|
```
|
|
143
355
|
|
|
356
|
+
Useful for CI systems.
|
|
357
|
+
|
|
358
|
+
## CI/CD Example
|
|
359
|
+
|
|
360
|
+
Validate environment configuration during deployment.
|
|
361
|
+
|
|
362
|
+
Example **GitHub Actions step**
|
|
363
|
+
|
|
364
|
+
```yaml
|
|
365
|
+
- name: Validate Environment Variables
|
|
366
|
+
run: npx safe-env-check env.schema.js --env-file .env.production --strict
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
This prevents deployments with invalid configuration.
|
|
370
|
+
|
|
371
|
+
## TypeScript Type Safety
|
|
372
|
+
|
|
373
|
+
The returned object is fully typed.
|
|
374
|
+
|
|
375
|
+
```ts
|
|
376
|
+
const env = validateEnv(schema);
|
|
377
|
+
|
|
378
|
+
env.PORT; // number
|
|
379
|
+
env.NODE_ENV; // "development" | "production"
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
## When to Use safe-env-check
|
|
383
|
+
|
|
384
|
+
- Node.js APIs
|
|
385
|
+
- Microservices
|
|
386
|
+
- Docker containers
|
|
387
|
+
- CI/CD pipelines
|
|
388
|
+
- Serverless functions
|
|
389
|
+
- Monorepos
|
|
390
|
+
- Multi-environment deployments
|
|
391
|
+
|
|
144
392
|
## License
|
|
145
393
|
|
|
146
394
|
MIT © Shakhawat Hossain
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|