@bb-labs/zod-env 0.0.1 → 0.0.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 +35 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
## Introduction
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Type-safe environment variable validation using [Zod](https://zod.dev).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
4
6
|
|
|
5
7
|
```bash
|
|
6
|
-
|
|
8
|
+
npm install @bb-labs/zod-env
|
|
7
9
|
```
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { zEnv } from "@bb-labs/zod-env";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
const env = zEnv(process.env, {
|
|
18
|
+
DATABASE_URL: z.string().url(),
|
|
19
|
+
PORT: z.coerce.number().default(3000),
|
|
20
|
+
NODE_ENV: z.enum(["development", "production", "test"]),
|
|
21
|
+
DEBUG: z.string().optional(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Fully typed!
|
|
25
|
+
env.DATABASE_URL; // string
|
|
26
|
+
env.PORT; // number
|
|
27
|
+
env.NODE_ENV; // "development" | "production" | "test"
|
|
28
|
+
env.DEBUG; // string | undefined
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Error Handling
|
|
32
|
+
|
|
33
|
+
If validation fails, `zEnv` throws an error with all validation issues:
|
|
10
34
|
|
|
11
|
-
```bash
|
|
12
|
-
bun run index.ts
|
|
13
35
|
```
|
|
36
|
+
Error: Environment validation failed:
|
|
37
|
+
DATABASE_URL: Invalid url
|
|
38
|
+
NODE_ENV: Invalid enum value. Expected 'development' | 'production' | 'test', received 'staging'
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## License
|
|
14
42
|
|
|
15
|
-
|
|
43
|
+
MIT
|