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_SIZEis{"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_DBandAPP_DB__URL) is aVldEnvError::Sourceerror.
§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§
- VldEnv
Error - Error type for env validation.
Functions§
- from_
dotenv - Validate a
.envfile (no prefix) intoT, without touching the process environment. To layer it under the process env, passread_dotenv(path)?.into_iter().chain(std::env::vars())tofrom_vars. - from_
env - Validate the whole process environment (no prefix) into
T. - from_
env_ prefixed - Validate the process variables starting with
prefix(stripped) intoT. - from_
vars - Validate
(name, value)pairs intoT, without a prefix. - from_
vars_ prefixed - Validate the
(name, value)pairs starting withprefix(stripped) intoT. Whatfrom_env_prefixeddoes with the process environment. - read_
dotenv - Read a
.envfile into(name, value)pairs (in file order) without touching the process environment.${VAR}references are expanded as dotenvy does.