Customization

Below are some examples of how you can customize the behavior of the library. 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.

src/env.ts
import { createEnv } from "@nobara/core";

export const env = createEnv({
  // ...
  // Tell the library to skip validation if condition is true.
  skipValidation: false,
});

Overriding the default error handler

src/env.ts
import { createEnv } from "@nobara/core";
import { type ValiError, flatten } from "valibot";

export const env = createEnv({
  // ...
  // Called when the schema validation fails.
  onValidationError: (error: ValiError) => {
    console.error("❌ Invalid environment variables:", flatten(error));
    throw new Error("Invalid environment variables");
  },
  // Called when server variables are accessed on the client.
  onInvalidAccess: (variable: string) => {
    throw new Error(
      "❌ Attempted to access a server-side environment variable on the client",
    );
  },
});

Tell when we're in a server context

src/env.ts
import { createEnv } from "@nobara/core";

export const env = createEnv({
  // ...
  // Tell the library when we're in a server context.
  isServer: typeof window === "undefined",
});