node-safe-env 0.1.2 → 0.1.3
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 +147 -57
- package/package.json +12 -1
package/README.md
CHANGED
|
@@ -1,23 +1,49 @@
|
|
|
1
1
|
# node-safe-env
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
3
8
|
Schema-based environment validation for Node.js and TypeScript.
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
Validate environment variables at startup, parse them into runtime types, and catch configuration problems before your app boots.
|
|
6
11
|
|
|
7
12
|
## Why node-safe-env?
|
|
8
13
|
|
|
9
|
-
- Fail
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
|
|
14
|
+
- Fail fast during application startup.
|
|
15
|
+
- Parse raw env strings into typed runtime values.
|
|
16
|
+
- See aggregated validation issues in one error report.
|
|
17
|
+
- Validate `.env` and `.env.example` usage from scripts or CI.
|
|
18
|
+
- Inspect where values came from with debug tracing.
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- Schema-based environment validation
|
|
23
|
+
- TypeScript-friendly schema inference
|
|
24
|
+
- Automatic parsing for numbers, booleans, arrays, dates
|
|
25
|
+
- Nested schemas with flattened env keys
|
|
26
|
+
- CLI validation for env and `.env.example`
|
|
27
|
+
- Aggregated validation errors
|
|
28
|
+
- Debug tracing for env source resolution
|
|
15
29
|
|
|
16
|
-
## Try
|
|
30
|
+
## Try it instantly
|
|
31
|
+
|
|
32
|
+
You can run the CLI without installing the package globally. The quickest way to try `node-safe-env` is with `npx`.
|
|
17
33
|
|
|
18
34
|
```bash
|
|
19
35
|
npx node-safe-env --help
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Validate environment variables:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
20
41
|
npx node-safe-env validate --schema ./env.schema.ts
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Validate `.env.example`:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
21
47
|
npx node-safe-env validate-example --schema ./env.schema.ts
|
|
22
48
|
```
|
|
23
49
|
|
|
@@ -27,6 +53,15 @@ npx node-safe-env validate-example --schema ./env.schema.ts
|
|
|
27
53
|
npm install node-safe-env
|
|
28
54
|
```
|
|
29
55
|
|
|
56
|
+
## Examples
|
|
57
|
+
|
|
58
|
+
Runnable examples are available in the repository and mirror common usage patterns.
|
|
59
|
+
|
|
60
|
+
- `examples/basic` - minimal schema validation
|
|
61
|
+
- `examples/nested` - nested schema with flattened env keys
|
|
62
|
+
- `examples/advanced` - arrays, custom parsing, debug mode, masking
|
|
63
|
+
- `examples/cli-schema` - schema module intended for CLI validation
|
|
64
|
+
|
|
30
65
|
## Quick Start
|
|
31
66
|
|
|
32
67
|
Define a schema:
|
|
@@ -62,7 +97,18 @@ What this gives you:
|
|
|
62
97
|
- `required: true` means the value must exist.
|
|
63
98
|
- `default` is used when a value is missing.
|
|
64
99
|
- `type` controls parsing and validation.
|
|
65
|
-
- returned `env` is strongly typed from your schema.
|
|
100
|
+
- The returned `env` object is strongly typed from your schema.
|
|
101
|
+
|
|
102
|
+
Example `.env`:
|
|
103
|
+
|
|
104
|
+
```env
|
|
105
|
+
APP_NAME=DemoApp
|
|
106
|
+
PORT=4000
|
|
107
|
+
DEBUG=true
|
|
108
|
+
NODE_ENV=development
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Values are read as strings from the environment and then parsed according to your schema.
|
|
66
112
|
|
|
67
113
|
## What Is a Schema in node-safe-env?
|
|
68
114
|
|
|
@@ -70,12 +116,12 @@ A schema is an object where each key maps to a rule object.
|
|
|
70
116
|
|
|
71
117
|
Each rule describes:
|
|
72
118
|
|
|
73
|
-
-
|
|
74
|
-
-
|
|
75
|
-
-
|
|
76
|
-
-
|
|
119
|
+
- What type the value should be with `type`
|
|
120
|
+
- Whether it must be present with `required`
|
|
121
|
+
- What to use if it is missing with `default`
|
|
122
|
+
- Any rule-specific options such as enum values, array settings, or a custom parser
|
|
77
123
|
|
|
78
|
-
Use `defineEnv()` when you want better literal type inference and reusable exported schema
|
|
124
|
+
Use `defineEnv()` when you want better literal type inference and a reusable exported schema module.
|
|
79
125
|
|
|
80
126
|
```ts
|
|
81
127
|
// env.schema.ts
|
|
@@ -87,13 +133,80 @@ export const schema = defineEnv({
|
|
|
87
133
|
} as const);
|
|
88
134
|
```
|
|
89
135
|
|
|
136
|
+
## When to use node-safe-env
|
|
137
|
+
|
|
138
|
+
Use this library if you want to:
|
|
139
|
+
|
|
140
|
+
- validate environment variables at application startup
|
|
141
|
+
- enforce typed configuration in Node.js or TypeScript projects
|
|
142
|
+
- ensure `.env.example` stays aligned with your configuration schema
|
|
143
|
+
- detect configuration issues early in CI pipelines
|
|
144
|
+
|
|
145
|
+
## Common Use Cases
|
|
146
|
+
|
|
147
|
+
- App startup validation: load and validate env once during bootstrap.
|
|
148
|
+
- CI checks for `.env.example`: fail pull requests when required keys are missing or stale.
|
|
149
|
+
- Debugging source issues: use debug output to understand where values were loaded from.
|
|
150
|
+
|
|
151
|
+
## Value at a Glance
|
|
152
|
+
|
|
153
|
+
`node-safe-env` is a schema-based env validator with:
|
|
154
|
+
|
|
155
|
+
- TypeScript-friendly schema inference
|
|
156
|
+
- CLI commands for runtime and `.env.example` validation
|
|
157
|
+
- Structured, aggregated validation errors
|
|
158
|
+
- Source tracing and debug visibility
|
|
159
|
+
|
|
160
|
+
## Nested Schemas
|
|
161
|
+
|
|
162
|
+
Schemas can be nested, but environment variable names are always flattened. Keys are generated by joining object path segments with `_` and converting each segment to uppercase. The library does not convert camelCase to snake_case, so `server.allowedHosts` becomes `SERVER_ALLOWEDHOSTS`, not `SERVER_ALLOWED_HOSTS`.
|
|
163
|
+
|
|
164
|
+
Examples:
|
|
165
|
+
|
|
166
|
+
- `server.port` -> `SERVER_PORT`
|
|
167
|
+
- `database.url` -> `DATABASE_URL`
|
|
168
|
+
- `server.allowedHosts` -> `SERVER_ALLOWEDHOSTS`
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const env = createEnv({
|
|
172
|
+
server: {
|
|
173
|
+
port: { type: "port", default: 3000 },
|
|
174
|
+
allowedHosts: { type: "array", required: true },
|
|
175
|
+
},
|
|
176
|
+
database: {
|
|
177
|
+
url: { type: "string", required: true },
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
console.log(env.server.port);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
```env
|
|
185
|
+
SERVER_PORT=4000
|
|
186
|
+
SERVER_ALLOWEDHOSTS=localhost,example.com
|
|
187
|
+
DATABASE_URL=postgres://localhost:5432/app
|
|
188
|
+
```
|
|
189
|
+
|
|
90
190
|
## CLI
|
|
91
191
|
|
|
92
|
-
`node-safe-env` includes:
|
|
192
|
+
`node-safe-env` includes two CLI commands:
|
|
93
193
|
|
|
94
194
|
- `validate`: validate current environment values against your schema
|
|
95
195
|
- `validate-example`: validate `.env.example` coverage against your schema
|
|
96
196
|
|
|
197
|
+
You can run the CLI without installing the package globally:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
npx node-safe-env --help
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Most common commands:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
npx node-safe-env validate --schema ./env.schema.ts
|
|
207
|
+
npx node-safe-env validate-example --schema ./env.schema.ts
|
|
208
|
+
```
|
|
209
|
+
|
|
97
210
|
```bash
|
|
98
211
|
node-safe-env validate --schema ./env.schema.ts
|
|
99
212
|
node-safe-env validate-example --schema ./env.schema.ts
|
|
@@ -109,11 +222,21 @@ The `--schema` module supports both JavaScript and TypeScript files:
|
|
|
109
222
|
|
|
110
223
|
Accepted export shapes:
|
|
111
224
|
|
|
112
|
-
-
|
|
113
|
-
-
|
|
225
|
+
- Default export
|
|
226
|
+
- Named export `schema`
|
|
114
227
|
|
|
115
228
|
## `.env.example` Validation
|
|
116
229
|
|
|
230
|
+
`.env.example` should contain all keys defined in your schema so new environments and CI checks stay aligned with your application configuration.
|
|
231
|
+
|
|
232
|
+
Example `.env.example`:
|
|
233
|
+
|
|
234
|
+
```env
|
|
235
|
+
PORT=3000
|
|
236
|
+
DEBUG=false
|
|
237
|
+
NODE_ENV=development
|
|
238
|
+
```
|
|
239
|
+
|
|
117
240
|
Validate a real file:
|
|
118
241
|
|
|
119
242
|
```ts
|
|
@@ -135,21 +258,6 @@ const issues = validateExampleEnv(schema, {
|
|
|
135
258
|
});
|
|
136
259
|
```
|
|
137
260
|
|
|
138
|
-
## Common Use Cases
|
|
139
|
-
|
|
140
|
-
- App startup validation: load and validate env once during bootstrap.
|
|
141
|
-
- CI checks for `.env.example`: fail PRs when required keys are missing or stale.
|
|
142
|
-
- Debugging source issues: use debug/tracing output to understand where values were loaded from.
|
|
143
|
-
|
|
144
|
-
## Value at a Glance
|
|
145
|
-
|
|
146
|
-
`node-safe-env` is a schema-based env validator with:
|
|
147
|
-
|
|
148
|
-
- TypeScript-friendly schema inference
|
|
149
|
-
- CLI commands for runtime and `.env.example` validation
|
|
150
|
-
- structured, aggregated validation errors
|
|
151
|
-
- source tracing and debug visibility
|
|
152
|
-
|
|
153
261
|
## Rule Types
|
|
154
262
|
|
|
155
263
|
Supported `type` values:
|
|
@@ -168,38 +276,20 @@ Supported `type` values:
|
|
|
168
276
|
- `date`
|
|
169
277
|
- `custom`
|
|
170
278
|
|
|
171
|
-
## Nested Schemas
|
|
172
|
-
|
|
173
|
-
Schemas can be nested. Env keys are flattened with `_` between segments.
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
const env = createEnv({
|
|
177
|
-
server: {
|
|
178
|
-
port: { type: "port", default: 3000 },
|
|
179
|
-
},
|
|
180
|
-
database: {
|
|
181
|
-
url: { type: "string", required: true },
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
```env
|
|
187
|
-
SERVER_PORT=4000
|
|
188
|
-
DATABASE_URL=postgres://localhost:5432/app
|
|
189
|
-
```
|
|
190
|
-
|
|
191
279
|
## Loading Order
|
|
192
280
|
|
|
193
|
-
When `options.source` is not provided, values are merged in this order
|
|
281
|
+
When `options.source` is not provided, values are merged in this order and later sources win:
|
|
194
282
|
|
|
195
283
|
1. `.env`
|
|
196
284
|
2. `.env.local`
|
|
197
285
|
3. `.env.<NODE_ENV>`
|
|
198
|
-
4.
|
|
286
|
+
4. Custom file from `options.envFile`
|
|
199
287
|
5. `process.env`
|
|
200
288
|
|
|
201
289
|
## Debug Mode
|
|
202
290
|
|
|
291
|
+
Use debug mode to inspect how values were loaded and resolved.
|
|
292
|
+
|
|
203
293
|
```ts
|
|
204
294
|
import { createEnv, type EnvDebugReport } from "node-safe-env";
|
|
205
295
|
|
|
@@ -212,7 +302,7 @@ createEnv(schema, {
|
|
|
212
302
|
});
|
|
213
303
|
```
|
|
214
304
|
|
|
215
|
-
Or:
|
|
305
|
+
Or log debug reports directly:
|
|
216
306
|
|
|
217
307
|
```ts
|
|
218
308
|
createEnv(schema, { debug: true });
|
|
@@ -224,7 +314,7 @@ createEnv(schema, { debug: true });
|
|
|
224
314
|
createEnv(schema, { strict: true });
|
|
225
315
|
```
|
|
226
316
|
|
|
227
|
-
Strict mode reports unknown environment keys as issues.
|
|
317
|
+
Strict mode reports unknown environment keys as validation issues.
|
|
228
318
|
|
|
229
319
|
## Error Handling
|
|
230
320
|
|
|
@@ -289,4 +379,4 @@ npm run check
|
|
|
289
379
|
|
|
290
380
|
## License
|
|
291
381
|
|
|
292
|
-
MIT
|
|
382
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-safe-env",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A tiny schema-based env loader and validator for Node.js.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"env",
|
|
7
|
+
"environment",
|
|
8
|
+
"environment variables",
|
|
7
9
|
"dotenv",
|
|
10
|
+
"env validation",
|
|
8
11
|
"config",
|
|
12
|
+
"configuration",
|
|
13
|
+
"node env",
|
|
14
|
+
"env schema",
|
|
15
|
+
"typescript env",
|
|
16
|
+
"env validator",
|
|
17
|
+
"env loader",
|
|
18
|
+
"env parser",
|
|
19
|
+
"node configuration",
|
|
9
20
|
"validator",
|
|
10
21
|
"node"
|
|
11
22
|
],
|