Typed environment variable validation with batch error reporting for Rust
cargo add philiprehberger-env-validatorTyped environment variable validation with batch error reporting for Rust
[dependencies]
philiprehberger-env-validator = "0.4.0"
use philiprehberger_env_validator::Schema;
let config = Schema::new()
.string("DATABASE_URL").build()
.integer("PORT").default_value("3000").build()
.boolean("DEBUG").default_value("false").build()
.validate()?;
let port = config["PORT"].as_int().unwrap();
let config = Schema::new()
.float("RATE_LIMIT").default_value("1.5").build()
.url("API_ENDPOINT").build()
.validate()?;
let rate = config["RATE_LIMIT"].as_float().unwrap();
let endpoint = config["API_ENDPOINT"].as_str().unwrap();
let config = Schema::new()
.string("APP_ENV").choices(&["development", "staging", "production"]).build()
.validate()?;
let config = Schema::new()
.integer("PORT").min(1024.0).max(65535.0).build()
.float("RATE").min(0.0).max(1.0).build()
.validate()?;
let config = Schema::new()
.string("PASSWORD").min_length(8).max_length(64).build()
.validate()?;
use std::collections::HashMap;
let mut source = HashMap::new();
source.insert("PORT".to_string(), "8080".to_string());
let config = Schema::new()
.integer("PORT").build()
.validate_from(Some(&source))?;
match schema.validate() {
Ok(config) => { /* use config */ }
Err(e) => {
for error in &e.errors {
eprintln!("{}", error);
}
}
}
use philiprehberger_env_validator::EnvValue;
let val: EnvValue = "hello".into();
let val: EnvValue = 42i64.into();
let val: EnvValue = 3.14f64.into();
let val: EnvValue = true.into();
// Display
println!("{}", val); // "true"
// Compare
assert_eq!(EnvValue::from(42i64), EnvValue::Int(42));
| Function / Type | Description |
|---|---|
Schema::new() | Create a new empty validation schema |
schema.string(name) | Add a string field to the schema |
schema.integer(name) | Add an integer field to the schema |
schema.float(name) | Add a float field to the schema |
schema.boolean(name) | Add a boolean field to the schema |
schema.url(name) | Add a URL field to the schema |
builder.required(bool) | Set whether the field is required (default: true) |
builder.default_value(v) | Set a default value for the field |
builder.choices(list) | Restrict allowed values to a set of choices |
builder.min(v) / builder.max(v) | Numeric range bounds (integer / float) |
builder.min_length(n) / builder.max_length(n) | String length bounds (string / url) |
builder.build() | Finalize the field and return the schema |
schema.validate() | Validate from environment variables |
schema.validate_from(source) | Validate from a custom HashMap source |
schema.field_count() | Number of fields registered in the schema |
EnvValue | Enum: Str, Int, Float, Bool |
EnvValue.as_string() | Render as String regardless of variant |
ValidationError | Error containing a Vec<String> of all failures |
cargo test
cargo clippy -- -D warnings
If you find this project useful: