envzod 1.0.0 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### CLI
6
+ - `npx envzod check` — validate env before deployment, works in CI/CD pipelines
7
+ - `--env` flag — specify custom env file path (default: `.env`)
8
+ - `--config` flag — specify custom config file path (default: `envzod.config`)
9
+ - TypeScript config support — CLI auto-detects `envzod.config.ts` before `.js`
10
+
11
+ ### Next.js helper (`envzod/next`)
12
+ - `createNextEnv` — Next.js-specific helper with server/client schema split
13
+ - `server` schema — auto-sourced from `process.env`, no key repetition needed
14
+ - `client` schema — explicit `runtimeEnv` only for `NEXT_PUBLIC_*` keys (webpack/Turbopack requirement)
15
+ - `bail` option — call `process.exit(1)` instead of throwing to avoid Next.js stack trace noise (default: `false`)
16
+
17
+ ### Improvements
18
+ - Validation errors now include full field details in the thrown error — visible in Next.js error overlays, not just terminal
19
+
20
+ ---
21
+
22
+ ## 1.0.1
23
+
24
+ - Fixed Next.js support — added `source` option to explicitly pass `process.env` keys
25
+ - Without `source`, Next.js/Turbopack could not statically inline `NEXT_PUBLIC_*` vars on the client side
26
+
27
+ ---
28
+
3
29
  ## 1.0.0 — Initial Release
4
30
 
5
31
  - `createEnv(schema, options?)` — validate environment variables against a Zod schema
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Sridhar-C-25
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sridhar-C-25
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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
- - `zod-env` validates your env at boot and gives you a **fully typed object** — no casting needed
13
+ - `envzod` validates your env at boot and gives you a **fully typed object** — no casting needed
14
14
 
15
15
  ---
16
16
 
@@ -32,31 +32,31 @@ yarn add envzod
32
32
 
33
33
  ```ts
34
34
  // env.ts
35
- import { createEnv } from 'envzod'
36
- import { z } from 'zod'
35
+ import { createEnv } from "envzod";
36
+ import { z } from "zod";
37
37
 
38
38
  export const env = createEnv({
39
39
  DATABASE_URL: z.string().url(),
40
40
  PORT: z.coerce.number().default(3000),
41
- NODE_ENV: z.enum(['development', 'test', 'production']),
41
+ NODE_ENV: z.enum(["development", "test", "production"]),
42
42
  JWT_SECRET: z.string().min(32),
43
- })
43
+ });
44
44
 
45
45
  // Fully typed — no casting needed
46
- env.PORT // number
47
- env.DATABASE_URL // string
48
- env.NODE_ENV // "development" | "test" | "production"
46
+ env.PORT; // number
47
+ env.DATABASE_URL; // string
48
+ env.NODE_ENV; // "development" | "test" | "production"
49
49
  ```
50
50
 
51
51
  ---
52
52
 
53
53
  ## Error Output
54
54
 
55
- When validation fails, `zod-env` prints a clear, readable error and throws:
55
+ When validation fails, `envzod` prints a clear, readable error and throws:
56
56
 
57
57
  ```
58
58
  ╔════════════════════════════════════════════╗
59
- ║ zod-env: Invalid Environment
59
+ ║ zod-env: Invalid Environment
60
60
  ╚════════════════════════════════════════════╝
61
61
 
62
62
  ✗ DATABASE_URL
@@ -74,6 +74,8 @@ When validation fails, `zod-env` prints a clear, readable error and throws:
74
74
  Fix the above and restart your server.
75
75
  ```
76
76
 
77
+ The thrown error includes the full formatted details — visible in Next.js error overlays, server logs, and CI output.
78
+
77
79
  ---
78
80
 
79
81
  ## Options
@@ -88,109 +90,220 @@ const env = createEnv(schema, {
88
90
 
89
91
  // Called with structured errors before throwing — use for Sentry, logging, etc.
90
92
  onError: (errors) => {
91
- Sentry.captureException(new Error('Invalid env'), { extra: { errors } })
93
+ Sentry.captureException(new Error("Invalid env"), { extra: { errors } });
92
94
  },
93
- })
95
+ });
94
96
  ```
95
97
 
96
98
  ### `onError` signature
97
99
 
98
100
  ```ts
99
101
  type EnvValidationError = {
100
- field: string
101
- message: string
102
- received: string | undefined
103
- }
102
+ field: string;
103
+ message: string;
104
+ received: string | undefined;
105
+ };
104
106
  ```
105
107
 
106
108
  ---
107
109
 
108
110
  ## Framework Examples
109
111
 
110
- ### Next.js (App Router)
112
+ ### Next.js (App Router) — `envzod/next`
113
+
114
+ Use `createNextEnv` from `envzod/next` for the best Next.js experience:
115
+
116
+ - **Server vars** are auto-sourced from `process.env` — no repetition needed
117
+ - **Client vars** (`NEXT_PUBLIC_*`) still require explicit `process.env.KEY` references — this is a hard webpack/Turbopack requirement, not a library limitation
118
+
119
+ **Step 1 — Define schema once in `envzod.config.ts`:**
111
120
 
112
121
  ```ts
113
- // src/env.ts
114
- import { createEnv } from 'envzod'
115
- import { z } from 'zod'
122
+ // envzod.config.ts
123
+ import { z } from "zod";
116
124
 
117
- export const env = createEnv({
125
+ export const server = {
118
126
  DATABASE_URL: z.string().url(),
119
127
  JWT_SECRET: z.string().min(32),
120
- NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
128
+ NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
129
+ };
130
+
131
+ export const client = {
121
132
  NEXT_PUBLIC_API_URL: z.string().url(),
122
- }, {
123
- verbose: process.env.NODE_ENV === 'development',
124
- })
133
+ NEXT_PUBLIC_APP_NAME: z.string().default("My App"),
134
+ };
135
+ ```
136
+
137
+ **Step 2 — Wire it up in `env.ts`:**
138
+
139
+ ```ts
140
+ // env.ts
141
+ import { createNextEnv } from "envzod/next";
142
+ import { server, client } from "./envzod.config";
143
+
144
+ export const env = createNextEnv({
145
+ server,
146
+ client,
147
+ runtimeEnv: {
148
+ // Only NEXT_PUBLIC_* vars needed here — server vars auto-sourced
149
+ NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
150
+ NEXT_PUBLIC_APP_NAME: process.env.NEXT_PUBLIC_APP_NAME,
151
+ },
152
+ verbose: process.env.NODE_ENV === "development",
153
+ // bail: true — clean process.exit(1) on error instead of throwing
154
+ });
155
+ ```
156
+
157
+ **Step 3 — CLI also reads `envzod.config.ts` automatically:**
158
+
159
+ ```bash
160
+ npx envzod check
125
161
  ```
126
162
 
163
+ #### `createNextEnv` options
164
+
165
+ | Option | Type | Default | Description |
166
+ | ------------ | --------------------------------- | --------- | -------------------------------------------------------- |
167
+ | `server` | `EnvSchema` | `{}` | Server-only vars, auto-sourced from `process.env` |
168
+ | `client` | `EnvSchema` | `{}` | Client vars, must all start with `NEXT_PUBLIC_` |
169
+ | `runtimeEnv` | `{ [K in keyof client]: string }` | required | Explicit `process.env.KEY` refs for each client key |
170
+ | `verbose` | `boolean` | `false` | Log success summary |
171
+ | `onError` | `(errors) => void` | — | Called with structured errors before throwing |
172
+ | `bail` | `boolean` | `false` | Call `process.exit(1)` instead of throwing — no Next.js stack trace noise |
173
+
174
+ ---
175
+
127
176
  ### Express
128
177
 
129
178
  ```ts
130
179
  // src/env.ts
131
- import { createEnv } from 'envzod'
132
- import { z } from 'zod'
133
-
134
- export const env = createEnv({
135
- PORT: z.coerce.number().default(3000),
136
- DATABASE_URL: z.string().url(),
137
- JWT_SECRET: z.string().min(32),
138
- }, {
139
- verbose: process.env.NODE_ENV === 'development',
140
- })
180
+ import { createEnv } from "envzod";
181
+ import { z } from "zod";
182
+
183
+ export const env = createEnv(
184
+ {
185
+ PORT: z.coerce.number().default(3000),
186
+ DATABASE_URL: z.string().url(),
187
+ JWT_SECRET: z.string().min(32),
188
+ },
189
+ {
190
+ verbose: process.env.NODE_ENV === "development",
191
+ },
192
+ );
141
193
 
142
194
  // app.ts
143
- import { env } from './env'
144
- app.listen(env.PORT)
195
+ import { env } from "./env";
196
+ app.listen(env.PORT);
145
197
  ```
146
198
 
147
199
  ### Bun
148
200
 
149
201
  ```ts
150
202
  // env.ts
151
- import { createEnv } from 'envzod'
152
- import { z } from 'zod'
153
-
154
- export const env = createEnv({
155
- PORT: z.coerce.number().default(3000),
156
- DATABASE_URL: z.string().url(),
157
- JWT_SECRET: z.string().min(32),
158
- }, {
159
- source: Bun.env,
160
- verbose: Bun.env.NODE_ENV === 'development',
161
- })
203
+ import { createEnv } from "envzod";
204
+ import { z } from "zod";
205
+
206
+ export const env = createEnv(
207
+ {
208
+ PORT: z.coerce.number().default(3000),
209
+ DATABASE_URL: z.string().url(),
210
+ JWT_SECRET: z.string().min(32),
211
+ },
212
+ {
213
+ source: Bun.env,
214
+ verbose: Bun.env.NODE_ENV === "development",
215
+ },
216
+ );
162
217
  ```
163
218
 
164
219
  ---
165
220
 
166
221
  ## TypeScript
167
222
 
168
- `zod-env` uses the `InferEnv<T>` utility type to derive the return type from your schema. No manual type annotations needed.
223
+ `envzod` uses the `InferEnv<T>` utility type to derive the return type from your schema. No manual type annotations needed.
169
224
 
170
225
  ```ts
171
- import type { InferEnv } from 'envzod'
172
- import { z } from 'zod'
226
+ import type { InferEnv } from "envzod";
227
+ import { z } from "zod";
173
228
 
174
229
  const schema = {
175
230
  PORT: z.coerce.number(),
176
- NODE_ENV: z.enum(['development', 'production']),
177
- }
231
+ NODE_ENV: z.enum(["development", "production"]),
232
+ };
178
233
 
179
- type Env = InferEnv<typeof schema>
234
+ type Env = InferEnv<typeof schema>;
180
235
  // { PORT: number; NODE_ENV: "development" | "production" }
181
236
  ```
182
237
 
183
238
  ---
184
239
 
240
+ ## CLI — `npx envzod check`
241
+
242
+ Validate your env before deploying — great for CI/CD pipelines.
243
+
244
+ **Supports both `.ts` and `.js` config files** — TypeScript config is auto-detected.
245
+
246
+ **1. Create `envzod.config.ts` (or `.js`) in your project root:**
247
+
248
+ ```ts
249
+ // envzod.config.ts
250
+ import { z } from "zod";
251
+
252
+ export default {
253
+ DATABASE_URL: z.string().url(),
254
+ PORT: z.coerce.number().default(3000),
255
+ NODE_ENV: z.enum(["development", "test", "production"]),
256
+ };
257
+ ```
258
+
259
+ Or CommonJS:
260
+
261
+ ```js
262
+ // envzod.config.js
263
+ const { z } = require("zod");
264
+
265
+ module.exports = {
266
+ DATABASE_URL: z.string().url(),
267
+ PORT: z.coerce.number().default(3000),
268
+ NODE_ENV: z.enum(["development", "test", "production"]),
269
+ };
270
+ ```
271
+
272
+ **2. Run the check:**
273
+
274
+ ```bash
275
+ npx envzod check
276
+ ```
277
+
278
+ **Options:**
279
+
280
+ ```bash
281
+ npx envzod check --env .env.production # custom env file (default: .env)
282
+ npx envzod check --config my.config # custom config file (tries .ts then .js)
283
+ ```
284
+
285
+ **Add to CI (GitHub Actions example):**
286
+
287
+ ```yaml
288
+ - name: Validate environment
289
+ run: npx envzod check --env .env.production
290
+ ```
291
+
292
+ ---
293
+
185
294
  ## vs t3-env
186
295
 
187
- | Feature | zod-env | t3-env |
188
- |---|---|---|
189
- | Framework | Universal | Next.js focused |
190
- | Setup | `createEnv(schema)` | Separate client/server schemas |
191
- | Dependencies | zod only | Next.js types + more |
192
- | Bundle | CJS + ESM | ESM only |
193
- | Error output | Pretty box | Zod default |
296
+ | Feature | envzod | t3-env |
297
+ | ---------------- | ------------------------------- | ------------------------------ |
298
+ | Framework | Universal | Next.js focused |
299
+ | Basic setup | `createEnv(schema)` | Separate client/server schemas |
300
+ | Next.js helper | `createNextEnv` via `envzod/next` | Built-in |
301
+ | Server/client split | Yes — `server` + `client` | Yes `server` + `client` |
302
+ | Source repetition | Server: none, Client: required | Always required (`runtimeEnv`) |
303
+ | CLI check | `npx envzod check` (TS + JS) | None |
304
+ | Dependencies | zod only | Next.js types + more |
305
+ | Bundle | CJS + ESM | ESM only |
306
+ | Error output | Pretty box with field details | Zod default |
194
307
 
195
308
  ---
196
309