If your project already uses Zod, you can passDocumentation Index
Fetch the complete documentation index at: https://next-safe-env.dev/llms.txt
Use this file to discover all available pages before exploring further.
z.object() schemas directly to createEnv instead of rewriting your validation logic using next-safe-env’s native validators. There is no peer dependency requirement - next-safe-env detects Zod schemas at runtime via structural typing (duck-typing), so installing next-safe-env will not pull Zod into projects that do not already have it.
Complete example
Mixing native and Zod schemas
You can use a Zod schema for one side of the split and nativenext-safe-env validators for the other. This is useful when you want to migrate incrementally or when the two schemas are maintained by different parts of your team:
What works with Zod
All standard Zod features work end-to-end when passed throughcreateEnv:
- Coercions -
z.coerce.number()parses the raw string value into a number, the same waynum()does natively. - Transforms -
z.string().transform(v => v.trim())runs after parsing and its output type is reflected in the inferred return type. - Defaults -
z.string().default('My App')is applied when the variable isundefined, the same as.default('My App')on a native validator. - Enums -
z.enum(['development', 'production', 'test'])narrows the inferred type to the literal union.
Error output
When a Zod field fails validation,next-safe-env maps the Zod error into the same ValidationFailure shape it uses for native validator failures. All errors appear together in the same pretty-printed output:
onValidationError callback and the same exit path as native validation failures.
All adapter rules still apply when you use Zod schemas. If you are using the
nextjs adapter, every key in client must be prefixed with NEXT_PUBLIC_. The adapter enforces this rule against the schema keys regardless of whether the schema is native or Zod-based. Server vars are still stripped from the returned object in browser context.You still need Zod installed in your own project -
next-safe-env will not install it for you. The “no peer dependency” guarantee means next-safe-env will not break if you do not have Zod installed; it simply will not recognize non-Zod objects as Zod schemas.
