@zodyac/zod-env
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Zod Environment parser

npm version

A part of Zodyac toolbox.

This package allows you to parse your .env file or process.env safely.

Installation

Install the package:

npm i @zodyac/env

Usage

Define your environment schema:

import { z } from 'zod';

const env_schema = z.object({
  // ... your zod schema
});

Don't forget to use z.coerce on types other than String!

Parse your environment using parse():

import { parse } from '@zodyac/env';
const env = parse(env_schema);

You can also use z.default() to provide a fallback.

Now your env infers types from Zod schema.

Configuration

By default, parse falls back to process.env, if no .env file was found.

You can ignore process.env fallback by adding:

const env = parse(env_schema, {
  ignoreProcessEnv: true,
});

Or you can provide it's path using path parameter:

const env = parse(env_schema, {
  path: 'path/to/env.file',
  ignoreProcessEnv: true,
});

License

MIT

Package Sidebar

Install

npm i @zodyac/zod-env

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

7.89 kB

Total Files

8

Last publish

Collaborators

  • bebrasmell