Libraries
    Preparing search index...

    Function parseEnv

    • Parses environment variables into a validated, typed config object.

      Every leaf field in the config descriptor must be wrapped with env() — this is enforced at the TypeScript level.

      The function:

      1. Walks the descriptor tree, reading raw values from source (defaults to process.env).
      2. Assembles a @cleverbrush/schema object schema from the leaf schemas.
      3. Validates and coerces the raw values via schema.validate().
      4. Returns the typed result or throws an EnvValidationError listing all missing and invalid variables.

      Type Parameters

      Parameters

      • config: T

        A descriptor tree where leaves are EnvFields and branches are plain objects.

      • Optionalsource: Record<string, string | undefined>

        The environment variable source. Defaults to process.env.

      Returns InferEnvConfig<T>

      const config = parseEnv({
      db: {
      host: env('DB_HOST', string().default('localhost')),
      port: env('DB_PORT', number().coerce().default(5432)),
      },
      debug: env('DEBUG', boolean().coerce().default(false)),
      });
    • Parses environment variables into a validated, typed config object.

      Every leaf field in the config descriptor must be wrapped with env() — this is enforced at the TypeScript level.

      The function:

      1. Walks the descriptor tree, reading raw values from source (defaults to process.env).
      2. Assembles a @cleverbrush/schema object schema from the leaf schemas.
      3. Validates and coerces the raw values via schema.validate().
      4. Returns the typed result or throws an EnvValidationError listing all missing and invalid variables.

      Type Parameters

      • T extends EnvConfig
      • C extends Record<string, unknown>

      Parameters

      • config: T

        A descriptor tree where leaves are EnvFields and branches are plain objects.

      • compute: (base: InferEnvConfig<T>) => C
      • Optionalsource: Record<string, string | undefined>

        The environment variable source. Defaults to process.env.

      Returns MergeTwo<InferEnvConfig<T>>

      const config = parseEnv({
      db: {
      host: env('DB_HOST', string().default('localhost')),
      port: env('DB_PORT', number().coerce().default(5432)),
      },
      debug: env('DEBUG', boolean().coerce().default(false)),
      });