Customization

Customize the behavior of validation with additional options.

Below are examples of how you can customize the behavior of envin. The default values are shown in the snippets below.

Skipping validation

Skipping validation is not encouraged and will lead to your types and runtime values being out of sync. It is available to let you opt out of validation during linting (or similar), or if you're building with Docker and not all environment variables are present when building the image.

env.config.ts
import {  } from "envin";

export default ({
  // ... your schema
  // Tell the library to skip validation if condition is true.
  : .. === "true",
});

When skipping validation, the default values are still used when possible. This is useful for development environments where you want to use the default values but still have the type safety.

Custom error handling

You can customize how validation errors and invalid access attempts are handled:

env.config.ts
import {  } from "envin";

export default ({
  // ... your schema
  // Called when the schema validation fails.
  :  => {
    .("❌ Invalid environment variables:", );
    .(1);
  },
  // Called when server variables are accessed on the client.
  :  => {
    throw new (
      `❌ Attempted to access server variable "${}" on the client`
    );
  },
});

Server context detection

Tell the library when we're in a server context. This is used to determine whether server-side environment variables should be accessible:

env.config.ts
import {  } from "envin";

export default ({
  // ... your schema
  // Tell the library when we're in a server context.
  : typeof  === "undefined",
});

Extending presets

Your env object may extend other presets by using the extends property. This can be used to include system environment variables for your deployment provider, or if you have a monorepo with multiple packages that share some environment variables.

env.config.ts
import {  } from "envin";
import {  } from "envin/presets/zod";

export const  = ({
  // ... your schema
  // Extend the Vercel preset.
  : [],
});

.VERCEL_URL;
VERCEL_URL?: string | undefined

envin ships the following presets out of the box, all importable from the /presets entrypoint:

Feel free to open a PR with more presets!

A preset is just like any other envin configuration object, so you can easily create your own:

env.config.ts
import {  } from "envin";
import {  } from "zod";

const  = {
  : "NEXT_PUBLIC_",
  : {
    : .().(1),
  },
  : {
    : .().(1),
    : .().(1),
  },
} as ;

const  = ({
  // ... your schema
  : [],
});

.STRIPE_SECRET_KEY;
type STRIPE_SECRET_KEY: string

Custom transformations and refinements

You can use custom transformations to process environment variables or add complex validation logic:

env.config.ts
import {  } from "envin";
import {  } from "zod";

const  = ({
  : {
    // Transform string to number with default
    : .().().("3000"),
    // Transform comma-separated string to array
    : .().( => .(",")),
    // Complex validation with refinement
    : 
      .()
      .()
      .(
         => .("postgresql://"),
        "Database URL must be a PostgreSQL connection string"
      ),
    // Conditional validation
    : .().(),
    : .().().(),
    : .().(1).(),
  },
  : .,
  // Custom schema transformation for complex interdependent validation
  : (, ) =>
    .().((, ) => {
      if (. || !) return { : true } as ;
      if (!. || !.) {
        .({
          : ..,
          : "EMAIL and PASSWORD are required if SKIP_AUTH is false",
        });
        return .;
      }
      return {
        : .,
        : .,
      };
    }),
});

.SKIP_AUTH;
SKIP_AUTH?: true | undefined

On this page