envzod 1.0.0 → 1.0.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 +27 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Works with **Next.js, Express, Fastify, Remix, Bun** — any Node.js project.
|
|
|
10
10
|
|
|
11
11
|
- `process.env` values are all `string | undefined` — no types, no validation
|
|
12
12
|
- Errors surface at runtime deep in your app instead of at startup
|
|
13
|
-
- `
|
|
13
|
+
- `envzod` validates your env at boot and gives you a **fully typed object** — no casting needed
|
|
14
14
|
|
|
15
15
|
---
|
|
16
16
|
|
|
@@ -52,7 +52,7 @@ env.NODE_ENV // "development" | "test" | "production"
|
|
|
52
52
|
|
|
53
53
|
## Error Output
|
|
54
54
|
|
|
55
|
-
When validation fails, `
|
|
55
|
+
When validation fails, `envzod` prints a clear, readable error and throws:
|
|
56
56
|
|
|
57
57
|
```
|
|
58
58
|
╔════════════════════════════════════════════╗
|
|
@@ -109,19 +109,34 @@ type EnvValidationError = {
|
|
|
109
109
|
|
|
110
110
|
### Next.js (App Router)
|
|
111
111
|
|
|
112
|
+
> **Important:** Next.js only inlines `NEXT_PUBLIC_*` vars when referenced **explicitly** (e.g. `process.env.NEXT_PUBLIC_FOO`). Passing the whole `process.env` object doesn't work on the client side. Always use the `source` option and list each var individually.
|
|
113
|
+
|
|
112
114
|
```ts
|
|
113
115
|
// src/env.ts
|
|
114
116
|
import { createEnv } from 'envzod'
|
|
115
117
|
import { z } from 'zod'
|
|
116
118
|
|
|
117
|
-
export const env = createEnv(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
export const env = createEnv(
|
|
120
|
+
{
|
|
121
|
+
// Server-only vars
|
|
122
|
+
DATABASE_URL: z.string().url(),
|
|
123
|
+
JWT_SECRET: z.string().min(32),
|
|
124
|
+
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
|
125
|
+
|
|
126
|
+
// Public vars (accessible in browser)
|
|
127
|
+
NEXT_PUBLIC_API_URL: z.string().url(),
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
// Must explicitly list each var so Next.js/Turbopack can statically inline them
|
|
131
|
+
source: {
|
|
132
|
+
DATABASE_URL: process.env.DATABASE_URL,
|
|
133
|
+
JWT_SECRET: process.env.JWT_SECRET,
|
|
134
|
+
NODE_ENV: process.env.NODE_ENV,
|
|
135
|
+
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
|
|
136
|
+
},
|
|
137
|
+
verbose: process.env.NODE_ENV === 'development',
|
|
138
|
+
},
|
|
139
|
+
)
|
|
125
140
|
```
|
|
126
141
|
|
|
127
142
|
### Express
|
|
@@ -165,7 +180,7 @@ export const env = createEnv({
|
|
|
165
180
|
|
|
166
181
|
## TypeScript
|
|
167
182
|
|
|
168
|
-
`
|
|
183
|
+
`envzod` uses the `InferEnv<T>` utility type to derive the return type from your schema. No manual type annotations needed.
|
|
169
184
|
|
|
170
185
|
```ts
|
|
171
186
|
import type { InferEnv } from 'envzod'
|
|
@@ -184,7 +199,7 @@ type Env = InferEnv<typeof schema>
|
|
|
184
199
|
|
|
185
200
|
## vs t3-env
|
|
186
201
|
|
|
187
|
-
| Feature |
|
|
202
|
+
| Feature | envzod | t3-env |
|
|
188
203
|
|---|---|---|
|
|
189
204
|
| Framework | Universal | Next.js focused |
|
|
190
205
|
| Setup | `createEnv(schema)` | Separate client/server schemas |
|
package/package.json
CHANGED