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/dist/index.d.mts CHANGED
@@ -1,25 +1,7 @@
1
- import { ZodTypeAny, z } from 'zod';
2
-
3
- /** A record of field names to Zod types */
4
- type EnvSchema = Record<string, ZodTypeAny>;
5
- /** Infer the output type of an env schema */
6
- type InferEnv<T extends EnvSchema> = {
7
- [K in keyof T]: z.infer<T[K]>;
8
- };
9
- interface EnvOptions<T extends EnvSchema> {
10
- /** Custom env source. Defaults to process.env */
11
- source?: Record<string, string | undefined>;
12
- /** Log a success summary table in development. Defaults to false */
13
- verbose?: boolean;
14
- /** Called with formatted error string before throwing. Use for custom logging/alerting */
15
- onError?: (errors: EnvValidationError[]) => void;
16
- }
17
- interface EnvValidationError {
18
- field: string;
19
- message: string;
20
- received: string | undefined;
21
- }
1
+ import { E as EnvSchema, a as EnvOptions, I as InferEnv } from './types-D3dpyeJh.mjs';
2
+ export { b as EnvValidationError } from './types-D3dpyeJh.mjs';
3
+ import 'zod';
22
4
 
23
5
  declare function createEnv<T extends EnvSchema>(schema: T, options?: EnvOptions<T>): InferEnv<T>;
24
6
 
25
- export { type EnvOptions, type EnvSchema, type EnvValidationError, type InferEnv, createEnv };
7
+ export { EnvOptions, EnvSchema, InferEnv, createEnv };
package/dist/index.d.ts CHANGED
@@ -1,25 +1,7 @@
1
- import { ZodTypeAny, z } from 'zod';
2
-
3
- /** A record of field names to Zod types */
4
- type EnvSchema = Record<string, ZodTypeAny>;
5
- /** Infer the output type of an env schema */
6
- type InferEnv<T extends EnvSchema> = {
7
- [K in keyof T]: z.infer<T[K]>;
8
- };
9
- interface EnvOptions<T extends EnvSchema> {
10
- /** Custom env source. Defaults to process.env */
11
- source?: Record<string, string | undefined>;
12
- /** Log a success summary table in development. Defaults to false */
13
- verbose?: boolean;
14
- /** Called with formatted error string before throwing. Use for custom logging/alerting */
15
- onError?: (errors: EnvValidationError[]) => void;
16
- }
17
- interface EnvValidationError {
18
- field: string;
19
- message: string;
20
- received: string | undefined;
21
- }
1
+ import { E as EnvSchema, a as EnvOptions, I as InferEnv } from './types-D3dpyeJh.js';
2
+ export { b as EnvValidationError } from './types-D3dpyeJh.js';
3
+ import 'zod';
22
4
 
23
5
  declare function createEnv<T extends EnvSchema>(schema: T, options?: EnvOptions<T>): InferEnv<T>;
24
6
 
25
- export { type EnvOptions, type EnvSchema, type EnvValidationError, type InferEnv, createEnv };
7
+ export { EnvOptions, EnvSchema, InferEnv, createEnv };
package/dist/index.js CHANGED
@@ -57,7 +57,7 @@ function createEnv(schema, options) {
57
57
  const formatted = formatErrors(result.errors);
58
58
  console.error(formatted);
59
59
  options?.onError?.(result.errors);
60
- throw new Error("zod-env: environment validation failed");
60
+ throw new Error(formatted);
61
61
  }
62
62
  if (options?.verbose) {
63
63
  console.log(formatSuccess(Object.keys(schema).length));
@@ -66,5 +66,3 @@ function createEnv(schema, options) {
66
66
  }
67
67
 
68
68
  exports.createEnv = createEnv;
69
- //# sourceMappingURL=index.js.map
70
- //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -55,7 +55,7 @@ function createEnv(schema, options) {
55
55
  const formatted = formatErrors(result.errors);
56
56
  console.error(formatted);
57
57
  options?.onError?.(result.errors);
58
- throw new Error("zod-env: environment validation failed");
58
+ throw new Error(formatted);
59
59
  }
60
60
  if (options?.verbose) {
61
61
  console.log(formatSuccess(Object.keys(schema).length));
@@ -64,5 +64,3 @@ function createEnv(schema, options) {
64
64
  }
65
65
 
66
66
  export { createEnv };
67
- //# sourceMappingURL=index.mjs.map
68
- //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,30 @@
1
+ import { E as EnvSchema, b as EnvValidationError, I as InferEnv } from './types-D3dpyeJh.mjs';
2
+ import 'zod';
3
+
4
+ interface NextEnvOptions<TServer extends EnvSchema, TClient extends EnvSchema> {
5
+ /** Server-only env vars — auto-sourced from process.env, never sent to the browser */
6
+ server?: TServer;
7
+ /** Client-safe env vars — must all start with NEXT_PUBLIC_ */
8
+ client?: TClient;
9
+ /**
10
+ * Explicit process.env references for each client key.
11
+ * Required because Next.js/webpack can only inline static process.env.KEY literals,
12
+ * not dynamic access. Server keys are auto-sourced and don't need to be listed here.
13
+ */
14
+ runtimeEnv: {
15
+ [K in keyof TClient]: string | undefined;
16
+ };
17
+ /** Log a success summary in development */
18
+ verbose?: boolean;
19
+ /** Called with validation errors before exiting */
20
+ onError?: (errors: EnvValidationError[]) => void;
21
+ /**
22
+ * Exit the process on validation failure instead of throwing.
23
+ * Default: false — throws an error normally.
24
+ * Set to true for a clean terminal output without Next.js stack trace noise.
25
+ */
26
+ bail?: boolean;
27
+ }
28
+ declare function createNextEnv<TServer extends EnvSchema = Record<never, never>, TClient extends EnvSchema = Record<never, never>>(options: NextEnvOptions<TServer, TClient>): InferEnv<TServer> & InferEnv<TClient>;
29
+
30
+ export { type NextEnvOptions, createNextEnv };
package/dist/next.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { E as EnvSchema, b as EnvValidationError, I as InferEnv } from './types-D3dpyeJh.js';
2
+ import 'zod';
3
+
4
+ interface NextEnvOptions<TServer extends EnvSchema, TClient extends EnvSchema> {
5
+ /** Server-only env vars — auto-sourced from process.env, never sent to the browser */
6
+ server?: TServer;
7
+ /** Client-safe env vars — must all start with NEXT_PUBLIC_ */
8
+ client?: TClient;
9
+ /**
10
+ * Explicit process.env references for each client key.
11
+ * Required because Next.js/webpack can only inline static process.env.KEY literals,
12
+ * not dynamic access. Server keys are auto-sourced and don't need to be listed here.
13
+ */
14
+ runtimeEnv: {
15
+ [K in keyof TClient]: string | undefined;
16
+ };
17
+ /** Log a success summary in development */
18
+ verbose?: boolean;
19
+ /** Called with validation errors before exiting */
20
+ onError?: (errors: EnvValidationError[]) => void;
21
+ /**
22
+ * Exit the process on validation failure instead of throwing.
23
+ * Default: false — throws an error normally.
24
+ * Set to true for a clean terminal output without Next.js stack trace noise.
25
+ */
26
+ bail?: boolean;
27
+ }
28
+ declare function createNextEnv<TServer extends EnvSchema = Record<never, never>, TClient extends EnvSchema = Record<never, never>>(options: NextEnvOptions<TServer, TClient>): InferEnv<TServer> & InferEnv<TClient>;
29
+
30
+ export { type NextEnvOptions, createNextEnv };
package/dist/next.js ADDED
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+
5
+ // src/format.ts
6
+ var BOX_WIDTH = 44;
7
+ function pad(text, width) {
8
+ return text + " ".repeat(Math.max(0, width - text.length));
9
+ }
10
+ function formatErrors(errors) {
11
+ const lines = [];
12
+ const title = " zod-env: Invalid Environment ";
13
+ lines.push(`\u2554${"\u2550".repeat(BOX_WIDTH)}\u2557`);
14
+ lines.push(`\u2551${pad(title, BOX_WIDTH)}\u2551`);
15
+ lines.push(`\u255A${"\u2550".repeat(BOX_WIDTH)}\u255D`);
16
+ lines.push("");
17
+ for (const err of errors) {
18
+ lines.push(` \u2717 ${err.field}`);
19
+ lines.push(` ${err.message}`);
20
+ if (err.received !== void 0) {
21
+ lines.push(` Got: "${err.received}"`);
22
+ }
23
+ lines.push("");
24
+ }
25
+ lines.push(" Fix the above and restart your server.");
26
+ return lines.join("\n");
27
+ }
28
+ function formatSuccess(count) {
29
+ return `\u2705 zod-env: ${count} variable${count === 1 ? "" : "s"} validated`;
30
+ }
31
+ function validate(schema, source) {
32
+ const shape = {};
33
+ for (const key of Object.keys(schema)) {
34
+ shape[key] = schema[key];
35
+ }
36
+ const zodObject = zod.z.object(shape);
37
+ const result = zodObject.safeParse(source);
38
+ if (result.success) {
39
+ return { success: true, data: result.data };
40
+ }
41
+ const errors = result.error.issues.map((issue) => {
42
+ const field = issue.path[0]?.toString() ?? "unknown";
43
+ return {
44
+ field,
45
+ message: issue.message,
46
+ received: source[field]
47
+ };
48
+ });
49
+ return { success: false, errors };
50
+ }
51
+
52
+ // src/next.ts
53
+ function createNextEnv(options) {
54
+ const {
55
+ server = {},
56
+ client = {},
57
+ runtimeEnv,
58
+ verbose,
59
+ onError,
60
+ bail = false
61
+ } = options;
62
+ for (const key of Object.keys(client)) {
63
+ if (!key.startsWith("NEXT_PUBLIC_")) {
64
+ throw new Error(
65
+ `envzod/next: client key "${key}" must start with NEXT_PUBLIC_.
66
+ Server-only keys belong in the "server" schema.`
67
+ );
68
+ }
69
+ }
70
+ const schema = { ...server, ...client };
71
+ const source = {
72
+ ...process.env,
73
+ ...runtimeEnv
74
+ };
75
+ const result = validate(schema, source);
76
+ if (!result.success) {
77
+ const formatted = formatErrors(result.errors);
78
+ console.error(formatted);
79
+ onError?.(result.errors);
80
+ if (bail) {
81
+ process.exit(1);
82
+ }
83
+ throw new Error(formatted);
84
+ }
85
+ if (verbose) {
86
+ console.log(formatSuccess(Object.keys(schema).length));
87
+ }
88
+ return result.data;
89
+ }
90
+
91
+ exports.createNextEnv = createNextEnv;
package/dist/next.mjs ADDED
@@ -0,0 +1,89 @@
1
+ import { z } from 'zod';
2
+
3
+ // src/format.ts
4
+ var BOX_WIDTH = 44;
5
+ function pad(text, width) {
6
+ return text + " ".repeat(Math.max(0, width - text.length));
7
+ }
8
+ function formatErrors(errors) {
9
+ const lines = [];
10
+ const title = " zod-env: Invalid Environment ";
11
+ lines.push(`\u2554${"\u2550".repeat(BOX_WIDTH)}\u2557`);
12
+ lines.push(`\u2551${pad(title, BOX_WIDTH)}\u2551`);
13
+ lines.push(`\u255A${"\u2550".repeat(BOX_WIDTH)}\u255D`);
14
+ lines.push("");
15
+ for (const err of errors) {
16
+ lines.push(` \u2717 ${err.field}`);
17
+ lines.push(` ${err.message}`);
18
+ if (err.received !== void 0) {
19
+ lines.push(` Got: "${err.received}"`);
20
+ }
21
+ lines.push("");
22
+ }
23
+ lines.push(" Fix the above and restart your server.");
24
+ return lines.join("\n");
25
+ }
26
+ function formatSuccess(count) {
27
+ return `\u2705 zod-env: ${count} variable${count === 1 ? "" : "s"} validated`;
28
+ }
29
+ function validate(schema, source) {
30
+ const shape = {};
31
+ for (const key of Object.keys(schema)) {
32
+ shape[key] = schema[key];
33
+ }
34
+ const zodObject = z.object(shape);
35
+ const result = zodObject.safeParse(source);
36
+ if (result.success) {
37
+ return { success: true, data: result.data };
38
+ }
39
+ const errors = result.error.issues.map((issue) => {
40
+ const field = issue.path[0]?.toString() ?? "unknown";
41
+ return {
42
+ field,
43
+ message: issue.message,
44
+ received: source[field]
45
+ };
46
+ });
47
+ return { success: false, errors };
48
+ }
49
+
50
+ // src/next.ts
51
+ function createNextEnv(options) {
52
+ const {
53
+ server = {},
54
+ client = {},
55
+ runtimeEnv,
56
+ verbose,
57
+ onError,
58
+ bail = false
59
+ } = options;
60
+ for (const key of Object.keys(client)) {
61
+ if (!key.startsWith("NEXT_PUBLIC_")) {
62
+ throw new Error(
63
+ `envzod/next: client key "${key}" must start with NEXT_PUBLIC_.
64
+ Server-only keys belong in the "server" schema.`
65
+ );
66
+ }
67
+ }
68
+ const schema = { ...server, ...client };
69
+ const source = {
70
+ ...process.env,
71
+ ...runtimeEnv
72
+ };
73
+ const result = validate(schema, source);
74
+ if (!result.success) {
75
+ const formatted = formatErrors(result.errors);
76
+ console.error(formatted);
77
+ onError?.(result.errors);
78
+ if (bail) {
79
+ process.exit(1);
80
+ }
81
+ throw new Error(formatted);
82
+ }
83
+ if (verbose) {
84
+ console.log(formatSuccess(Object.keys(schema).length));
85
+ }
86
+ return result.data;
87
+ }
88
+
89
+ export { createNextEnv };
@@ -0,0 +1,23 @@
1
+ import { ZodTypeAny, z } from 'zod';
2
+
3
+ /** A record of field names to Zod types */
4
+ type EnvSchema = Record<string, ZodTypeAny>;
5
+ /** Infer the output type of an env schema */
6
+ type InferEnv<T extends EnvSchema> = {
7
+ [K in keyof T]: z.infer<T[K]>;
8
+ };
9
+ interface EnvOptions<T extends EnvSchema> {
10
+ /** Custom env source. Defaults to process.env */
11
+ source?: Record<string, string | undefined>;
12
+ /** Log a success summary table in development. Defaults to false */
13
+ verbose?: boolean;
14
+ /** Called with formatted error string before throwing. Use for custom logging/alerting */
15
+ onError?: (errors: EnvValidationError[]) => void;
16
+ }
17
+ interface EnvValidationError {
18
+ field: string;
19
+ message: string;
20
+ received: string | undefined;
21
+ }
22
+
23
+ export type { EnvSchema as E, InferEnv as I, EnvOptions as a, EnvValidationError as b };
@@ -0,0 +1,23 @@
1
+ import { ZodTypeAny, z } from 'zod';
2
+
3
+ /** A record of field names to Zod types */
4
+ type EnvSchema = Record<string, ZodTypeAny>;
5
+ /** Infer the output type of an env schema */
6
+ type InferEnv<T extends EnvSchema> = {
7
+ [K in keyof T]: z.infer<T[K]>;
8
+ };
9
+ interface EnvOptions<T extends EnvSchema> {
10
+ /** Custom env source. Defaults to process.env */
11
+ source?: Record<string, string | undefined>;
12
+ /** Log a success summary table in development. Defaults to false */
13
+ verbose?: boolean;
14
+ /** Called with formatted error string before throwing. Use for custom logging/alerting */
15
+ onError?: (errors: EnvValidationError[]) => void;
16
+ }
17
+ interface EnvValidationError {
18
+ field: string;
19
+ message: string;
20
+ received: string | undefined;
21
+ }
22
+
23
+ export type { EnvSchema as E, InferEnv as I, EnvOptions as a, EnvValidationError as b };
package/package.json CHANGED
@@ -1,74 +1,86 @@
1
- {
2
- "name": "envzod",
3
- "version": "1.0.0",
4
- "description": "Universal, type-safe environment variable validation powered by Zod. Zero config, works everywhere.",
5
- "main": "./dist/index.js",
6
- "module": "./dist/index.mjs",
7
- "types": "./dist/index.d.ts",
8
- "sideEffects": false,
9
- "exports": {
10
- ".": {
11
- "types": "./dist/index.d.ts",
12
- "import": "./dist/index.mjs",
13
- "require": "./dist/index.js"
14
- }
15
- },
16
- "files": [
17
- "dist/index.js",
18
- "dist/index.mjs",
19
- "dist/index.d.ts",
20
- "dist/index.d.mts",
21
- "LICENSE",
22
- "CHANGELOG.md"
23
- ],
24
- "scripts": {
25
- "build": "tsup",
26
- "dev": "tsup --watch",
27
- "test": "vitest run",
28
- "test:watch": "vitest",
29
- "typecheck": "tsc --noEmit",
30
- "prepublishOnly": "npm run typecheck && npm test && npm run build"
31
- },
32
- "engines": {
33
- "node": ">=16.0.0"
34
- },
35
- "peerDependencies": {
36
- "zod": ">=3.0.0"
37
- },
38
- "peerDependenciesMeta": {
39
- "zod": {
40
- "optional": false
41
- }
42
- },
43
- "devDependencies": {
44
- "@types/node": "^20.0.0",
45
- "tsup": "^8.0.0",
46
- "typescript": "^5.0.0",
47
- "vitest": "^1.0.0",
48
- "zod": "^3.22.0"
49
- },
50
- "keywords": [
51
- "zod",
52
- "env",
53
- "environment",
54
- "validation",
55
- "typescript",
56
- "nodejs",
57
- "nextjs",
58
- "process.env",
59
- "env-validation",
60
- "type-safe",
61
- "dotenv",
62
- "schema"
63
- ],
64
- "author": "Sridhar-C-25",
65
- "license": "MIT",
66
- "repository": {
67
- "type": "git",
68
- "url": "https://github.com/Sridhar-C-25/zod-env"
69
- },
70
- "bugs": {
71
- "url": "https://github.com/Sridhar-C-25/zod-env/issues"
72
- },
73
- "homepage": "https://github.com/Sridhar-C-25/zod-env#readme"
74
- }
1
+ {
2
+ "name": "envzod",
3
+ "version": "1.1.0",
4
+ "description": "Universal, type-safe environment variable validation powered by Zod. Zero config, works everywhere.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.js"
14
+ },
15
+ "./next": {
16
+ "types": "./dist/next.d.ts",
17
+ "import": "./dist/next.mjs",
18
+ "require": "./dist/next.js"
19
+ }
20
+ },
21
+ "bin": {
22
+ "envzod": "./dist/cli.js"
23
+ },
24
+ "files": [
25
+ "dist/index.js",
26
+ "dist/index.mjs",
27
+ "dist/next.js",
28
+ "dist/next.mjs",
29
+ "dist/cli.js",
30
+ "dist/*.d.ts",
31
+ "dist/*.d.mts",
32
+ "LICENSE",
33
+ "CHANGELOG.md"
34
+ ],
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "dev": "tsup --watch",
38
+ "test": "vitest run",
39
+ "test:watch": "vitest",
40
+ "typecheck": "tsc --noEmit",
41
+ "prepublishOnly": "npm run typecheck && npm test && npm run build"
42
+ },
43
+ "engines": {
44
+ "node": ">=16.0.0"
45
+ },
46
+ "peerDependencies": {
47
+ "zod": ">=3.0.0"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "zod": {
51
+ "optional": false
52
+ }
53
+ },
54
+ "devDependencies": {
55
+ "@types/node": "^20.0.0",
56
+ "jiti": "^2.6.1",
57
+ "tsup": "^8.0.0",
58
+ "typescript": "^5.0.0",
59
+ "vitest": "^1.0.0",
60
+ "zod": "^3.22.0"
61
+ },
62
+ "keywords": [
63
+ "zod",
64
+ "env",
65
+ "environment",
66
+ "validation",
67
+ "typescript",
68
+ "nodejs",
69
+ "nextjs",
70
+ "process.env",
71
+ "env-validation",
72
+ "type-safe",
73
+ "dotenv",
74
+ "schema"
75
+ ],
76
+ "author": "Sridhar-C-25",
77
+ "license": "MIT",
78
+ "repository": {
79
+ "type": "git",
80
+ "url": "https://github.com/Sridhar-C-25/zod-env"
81
+ },
82
+ "bugs": {
83
+ "url": "https://github.com/Sridhar-C-25/zod-env/issues"
84
+ },
85
+ "homepage": "https://github.com/Sridhar-C-25/zod-env#readme"
86
+ }