@safe-hand/safe-env-check 1.1.0 → 1.1.2

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
@@ -4,15 +4,37 @@
4
4
  ![license](https://img.shields.io/npm/l/@safe-hand/safe-env-check)
5
5
  ![downloads](https://img.shields.io/npm/dm/@safe-hand/safe-env-check)
6
6
 
7
- A tiny TypeScript library to validate environment variables using a schema with support for:
7
+ A tiny TypeScript-first environment variable validator that ensures your application starts with a correct configuration.
8
8
 
9
- - Type validation
10
- - ✅ Required & default values
11
- - Enum values
12
- - ✅ Strict mode (no extra env vars)
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
- ## Schema Options
80
+ ### Schema Options
73
81
 
74
- Each environment variables supports the following options:
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
- ## Example
91
+ ### Example Schema
84
92
 
85
93
  ```ts
86
- DATABASE_URL: { type: "string", required: true },
87
- DEBUG: { type: "boolean", default: false },
88
- MODE: { type: "enum", values: ["dev", "prod"] }
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
+ };
89
103
  ```
90
104
 
91
- ## Strict Mode
105
+ ### Default Values
92
106
 
93
- Disallow environment variables that are not defined in the schema.
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
128
+
129
+ ```ts
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
+ }
176
+ ```
177
+
178
+ Invalid values will throw an error.
179
+
180
+ ### Strict Mode
181
+
182
+ Prevent unexpected environment variables.
94
183
 
95
184
  ```ts
96
185
  validateEnv(schema, { strict: true });
97
186
  ```
98
187
 
99
- If extra variables are found, validation will fail.
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
- ## Custom Error Formatter
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,22 @@ validateEnv(schema, {
108
253
  });
109
254
  ```
110
255
 
111
- ## Dotenv Support
256
+ ## Loading Environment Variables
257
+
258
+ `safe-env-check` does **not automatically load `.env` files**.
259
+
260
+ Load environment variables before calling `validateEnv`, for example using `dotenv`:
261
+
262
+ ```ts
263
+ import dotenv from "dotenv";
264
+ import { validateEnv } from "@safe-hand/safe-env-check";
265
+
266
+ dotenv.config();
112
267
 
113
- By default, the library loads .env automatically using dotenv.
268
+ const env = validateEnv(schema);
269
+ ```
114
270
 
115
- Example .env file:
271
+ **Example** `.env`:
116
272
 
117
273
  ```bash
118
274
  PORT=3000
@@ -120,9 +276,17 @@ JWT_SECRET=supersecret
120
276
  NODE_ENV=development
121
277
  ```
122
278
 
279
+ If you are using the CLI, you can provide a custom environment file:
280
+
281
+ ```bash
282
+ safe-env-check --env-file .env.test
283
+ ```
284
+
123
285
  ## CLI Usage
124
286
 
125
- Create a schema file called env.schema.js:
287
+ You can validate environment variables without writing code.
288
+
289
+ This is useful for CI/CD pipelines.
126
290
 
127
291
  ```ts
128
292
  module.exports = {
@@ -131,16 +295,105 @@ module.exports = {
131
295
  };
132
296
  ```
133
297
 
134
- Run validation:
298
+ **Run validation:**
135
299
 
136
- ```bash
300
+ ```cmd
137
301
  npx safe-env-check env.schema.js
302
+ ```
303
+
304
+ or
305
+
306
+ ```cmd
307
+ npx safe-env-check --schema env.schema.js
308
+ ```
309
+
310
+ ### CLI Options
311
+
312
+ | Flag | Description |
313
+ | --------------- | ---------------------------------------------------------------------------------------------- |
314
+ | `--schema` | schema file path |
315
+ | `--env-file` | load a custom env file |
316
+ | `--strict` | enable strict mode |
317
+ | `--prefix` | env variable prefix |
318
+ | `--format json` | output errors in JSON |
319
+ | `--quiet` | Suppress the success message "✅ Environment variables are valid" (errors still exit non-zero) |
320
+
321
+ |
322
+
323
+ ### CLI Examples
324
+
325
+ **Basic validation**
326
+
327
+ ```cmd
138
328
  npx safe-env-check env.schema.js
139
- npx safe-env-check --schema env.schema.js --strict
329
+ ```
330
+
331
+ **Using strict mode**
332
+
333
+ ```cmd
334
+ npx safe-env-check env.schema.js --strict
335
+ ```
336
+
337
+ **Using prefix**
338
+
339
+ ```cmd
340
+ npx safe-env-check env.schema.js --prefix API_
341
+ ```
342
+
343
+ **Suppress success message**
344
+
345
+ ```cmd
346
+ safe-env-check env.schema.js --quiet
347
+ ```
348
+
349
+ **Validate production env file**
350
+
351
+ ```cmd
140
352
  npx safe-env-check env.schema.js --env-file .env.production
353
+ ```
354
+
355
+ **JSON error output**
356
+
357
+ ```cmd
141
358
  npx safe-env-check env.schema.js --format json
142
359
  ```
143
360
 
361
+ Useful for CI systems.
362
+
363
+ ## CI/CD Example
364
+
365
+ Validate environment configuration during deployment.
366
+
367
+ Example **GitHub Actions step**
368
+
369
+ ```yaml
370
+ - name: Validate Environment Variables
371
+ run: npx safe-env-check env.schema.js --env-file .env.production --strict
372
+ ```
373
+
374
+ This prevents deployments with invalid configuration.
375
+
376
+ ## TypeScript Type Safety
377
+
378
+ The returned object is fully typed.
379
+
380
+ ```ts
381
+ const env = validateEnv(schema);
382
+
383
+ env.PORT; // number
384
+ env.NODE_ENV; // "development" | "production"
385
+ ```
386
+
387
+ ## When to Use safe-env-check
388
+
389
+ - Node.js APIs
390
+ - Microservices
391
+ - Docker containers
392
+ - CI/CD pipelines
393
+ - Serverless functions
394
+ - Monorepos
395
+ - Multi-environment deployments
396
+
144
397
  ## License
145
398
 
146
399
  MIT © Shakhawat Hossain
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node