Expand description
Derive macros for the premortem configuration validation library.
This crate provides the #[derive(Validate)] macro that generates
Validate trait implementations using stillwater’s Validation::all()
pattern for error accumulation.
§Basic Usage
ⓘ
use premortem::Validate;
#[derive(Validate)]
struct ServerConfig {
#[validate(non_empty)]
host: String,
#[validate(range(1..=65535))]
port: u16,
}§Available Validators
§String Validators
non_empty- Value cannot be emptymin_length(n)- Minimum string lengthmax_length(n)- Maximum string lengthlength(n..=m)- String length in rangepattern("regex")- Match regex patternemail- Valid email formaturl- Valid URL formatip- Valid IP addressuuid- Valid UUID format
§Numeric Validators
range(n..=m)- Value in rangepositive- Value > 0negative- Value < 0non_zero- Value != 0
§Path Validators
file_exists- File exists at pathdir_exists- Directory exists at pathparent_exists- Parent directory existsextension("ext")- File has extension
§Collection Validators
each(validator)- Apply validator to each element
§Structural Validators
nested- Validate nested structskip- Skip validation for this fieldcustom = "fn_name"- Call custom validation function
§Control Flow
when = "condition"- Conditional validationmessage = "custom message"- Override error message
§Sensitive Fields
Use #[sensitive] to redact field values in error messages:
ⓘ
#[derive(Validate)]
struct Credentials {
#[sensitive]
#[validate(min_length(8))]
password: String,
}§Struct-Level Validation
Use #[validate(custom = "fn_name")] on the struct for cross-field validation:
ⓘ
#[derive(Validate)]
#[validate(custom = "validate_config")]
struct Config {
start_port: u16,
end_port: u16,
}
fn validate_config(cfg: &Config) -> ConfigValidation<()> {
if cfg.start_port < cfg.end_port {
Validation::Success(())
} else {
Validation::fail_with(ConfigError::CrossFieldError {
paths: vec!["start_port".into(), "end_port".into()],
message: "start_port must be less than end_port".into(),
})
}
}Derive Macros§
- Validate
- Derive the
Validatetrait for a struct.