envzod 1.0.1 → 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
@@ -32,20 +32,20 @@ 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
  ---
@@ -56,7 +56,7 @@ 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, `envzod` 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,92 +90,130 @@ 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
111
118
 
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.
119
+ **Step 1 Define schema once in `envzod.config.ts`:**
113
120
 
114
121
  ```ts
115
- // src/env.ts
116
- import { createEnv } from 'envzod'
117
- import { z } from 'zod'
122
+ // envzod.config.ts
123
+ import { z } from "zod";
118
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
+ export const server = {
126
+ DATABASE_URL: z.string().url(),
127
+ JWT_SECRET: z.string().min(32),
128
+ NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
129
+ };
125
130
 
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',
131
+ export const client = {
132
+ NEXT_PUBLIC_API_URL: z.string().url(),
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,
138
151
  },
139
- )
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
140
161
  ```
141
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
+
142
176
  ### Express
143
177
 
144
178
  ```ts
145
179
  // src/env.ts
146
- import { createEnv } from 'envzod'
147
- import { z } from 'zod'
180
+ import { createEnv } from "envzod";
181
+ import { z } from "zod";
148
182
 
149
- export const env = createEnv({
150
- PORT: z.coerce.number().default(3000),
151
- DATABASE_URL: z.string().url(),
152
- JWT_SECRET: z.string().min(32),
153
- }, {
154
- verbose: process.env.NODE_ENV === 'development',
155
- })
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
+ );
156
193
 
157
194
  // app.ts
158
- import { env } from './env'
159
- app.listen(env.PORT)
195
+ import { env } from "./env";
196
+ app.listen(env.PORT);
160
197
  ```
161
198
 
162
199
  ### Bun
163
200
 
164
201
  ```ts
165
202
  // env.ts
166
- import { createEnv } from 'envzod'
167
- import { z } from 'zod'
203
+ import { createEnv } from "envzod";
204
+ import { z } from "zod";
168
205
 
169
- export const env = createEnv({
170
- PORT: z.coerce.number().default(3000),
171
- DATABASE_URL: z.string().url(),
172
- JWT_SECRET: z.string().min(32),
173
- }, {
174
- source: Bun.env,
175
- verbose: Bun.env.NODE_ENV === 'development',
176
- })
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
+ );
177
217
  ```
178
218
 
179
219
  ---
@@ -183,29 +223,87 @@ export const env = createEnv({
183
223
  `envzod` uses the `InferEnv<T>` utility type to derive the return type from your schema. No manual type annotations needed.
184
224
 
185
225
  ```ts
186
- import type { InferEnv } from 'envzod'
187
- import { z } from 'zod'
226
+ import type { InferEnv } from "envzod";
227
+ import { z } from "zod";
188
228
 
189
229
  const schema = {
190
230
  PORT: z.coerce.number(),
191
- NODE_ENV: z.enum(['development', 'production']),
192
- }
231
+ NODE_ENV: z.enum(["development", "production"]),
232
+ };
193
233
 
194
- type Env = InferEnv<typeof schema>
234
+ type Env = InferEnv<typeof schema>;
195
235
  // { PORT: number; NODE_ENV: "development" | "production" }
196
236
  ```
197
237
 
198
238
  ---
199
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
+
200
294
  ## vs t3-env
201
295
 
202
- | Feature | envzod | t3-env |
203
- |---|---|---|
204
- | Framework | Universal | Next.js focused |
205
- | Setup | `createEnv(schema)` | Separate client/server schemas |
206
- | Dependencies | zod only | Next.js types + more |
207
- | Bundle | CJS + ESM | ESM only |
208
- | 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 |
209
307
 
210
308
  ---
211
309