Skip to main content

Module env

Module env 

Source
Expand description

§vld_app::env — validate configuration from environment variables

Read environment variables (feature env, no dependency) or a .env file (feature dotenv, dotenvy 0.15) into a vld type at startup: a typed settings struct, or a VldEnvError naming every invalid variable.

§Quick Start

use vld_app::env::prelude::*;

vld::schema! {
    #[derive(Debug)]
    pub struct Db {
        pub url: String     => vld::string().min(1),
        pub pool_size: i64  => vld::number().int().min(1).with_default(10),
    }
}

vld::schema! {
    #[derive(Debug)]
    pub struct Settings {
        pub host: String => vld::string().min(1),
        pub port: i64    => vld::number().int().min(1).max(65535),
        pub debug: bool  => vld::boolean().with_default(false),
        pub db: Db       => vld::nested(Db::parse_value),
    }
}

// In an application: `from_env_prefixed::<Settings>("APP_")`.
let vars = [
    ("APP_HOST", "0.0.0.0"),
    ("APP_PORT", "8080"),
    ("APP_DB__URL", "postgres://localhost/app"),
    ("PATH", "/usr/bin"), // no prefix: ignored
];
let s: Settings = from_vars_prefixed(vars, "APP_").unwrap();
assert_eq!((s.host.as_str(), s.port, s.debug), ("0.0.0.0", 8080, false));
assert_eq!(s.db.pool_size, 10);

let r: Result<Settings, _> = from_vars_prefixed([("APP_PORT", "http")], "APP_");
let err = r.unwrap_err();
let vars: Vec<&str> = err.var_issues().map(|(var, _)| var).collect();
assert_eq!(vars, ["APP_HOST", "APP_PORT", "APP_DB"]);

§Variable names

  • The prefix is matched ASCII case-insensitively and stripped; other variables are ignored. The rest of the name is lower-cased and split on __ into nested objects: APP_DB__POOL_SIZE is {"db": {"pool_size": ..}}. Field names must therefore be lower snake case.
  • Names with an empty part (APP_, APP__X, APP_A__) and, for the process environment, names or values that are not UTF-8 are skipped.
  • When a name repeats, the later value wins (so in read_dotenv(p)?.into_iter().chain(std::env::vars()) the process env overrides the file).
  • A variable that is both a value and a section (APP_DB and APP_DB__URL) is a VldEnvError::Source error.

§Values

Each value is coerced with vld::schema::coerce_text (8080 → number, true → boolean, 01234 stays text) and the result parsed with vld::schema::parse_coerced, so a value follows its field’s schema: APP_HOST=12345 into vld::string() is "12345", and a single value into vld::array(..) is a one-element array.

Guide: https://s00d.github.io/vld/integrations/config-cli/env

Modules§

prelude
Prelude — re-exports everything you need.

Enums§

VldEnvError
Error type for env validation.

Functions§

from_dotenv
Validate a .env file (no prefix) into T, without touching the process environment. To layer it under the process env, pass read_dotenv(path)?.into_iter().chain(std::env::vars()) to from_vars.
from_env
Validate the whole process environment (no prefix) into T.
from_env_prefixed
Validate the process variables starting with prefix (stripped) into T.
from_vars
Validate (name, value) pairs into T, without a prefix.
from_vars_prefixed
Validate the (name, value) pairs starting with prefix (stripped) into T. What from_env_prefixed does with the process environment.
read_dotenv
Read a .env file into (name, value) pairs (in file order) without touching the process environment. ${VAR} references are expanded as dotenvy does.