Skip to main content

Crate vld_config

Crate vld_config 

Source
Expand description

§vld-config — Validate configuration files with vld

Load configuration from TOML, YAML, JSON, or environment variables and validate it against vld schemas at load time. Supports both config-rs and figment as backends.

§Quick Start (config-rs)

use vld::prelude::*;
use vld_config::from_config;

vld::schema! {
    #[derive(Debug)]
    pub struct AppSettings {
        pub host: String => vld::string().min(1),
        pub port: i64    => vld::number().int().min(1).max(65535),
    }
}

let config = config::Config::builder()
    .add_source(config::File::with_name("config"))
    .add_source(config::Environment::with_prefix("APP"))
    .build()
    .unwrap();

let settings: AppSettings = from_config(&config).unwrap();
println!("Listening on {}:{}", settings.host, settings.port);

§Quick Start (figment)

use vld::prelude::*;
use vld_config::from_figment;

vld::schema! {
    #[derive(Debug)]
    pub struct AppSettings {
        pub host: String => vld::string().min(1),
        pub port: i64    => vld::number().int().min(1).max(65535),
    }
}

let figment = figment::Figment::new()
    .merge(figment::providers::Serialized::defaults(
        serde_json::json!({"host": "0.0.0.0", "port": 3000}),
    ))
    .merge(figment::providers::Env::prefixed("APP_"));

let settings: AppSettings = from_figment(&figment).unwrap();

Modules§

prelude
Prelude — re-exports everything you need.

Enums§

VldConfigError
Error type for config validation.

Functions§

from_builder
Load and validate configuration from a config::ConfigBuilder.
from_config
Load and validate configuration from a config::Config instance.
from_value
Validate a raw serde_json::Value as if it came from a config source.