Skip to main content

validate

Function validate 

Source
pub fn validate(
    schema: &ConfigSchema,
    value: &ConfigValue,
) -> Result<(), SchemaError>
Expand description

Check value against schema, reporting the first failure with its path.

First failure, not all of them. A shape mismatch high in the tree makes everything under it meaningless, so a list of twelve consequential errors would bury the one that matters; the loader’s contract is to warn and keep going per key, and one clear message per key is what serves it.

Rules: scalars must match their kind exactly (no coercion); an ConfigSchema::Enum needs a ConfigValue::Str among its forms; a record rejects missing required fields AND unknown fields. Paths use .field for record fields and [i] for list indices.

§Examples

use lattice_config::schema::validate;
use lattice_config::{ConfigSchema, ConfigValue, SchemaField};

let schema = ConfigSchema::list(ConfigSchema::record([
    SchemaField::new("key", ConfigSchema::string(), ""),
    SchemaField::new("file", ConfigSchema::string(), ""),
]));
let good = ConfigValue::record([
    ("key".to_string(), ConfigValue::Str("t".into())),
    ("file".to_string(), ConfigValue::Str("todo.org".into())),
]);
let bad = ConfigValue::record([
    ("key".to_string(), ConfigValue::Str("n".into())),
    ("file".to_string(), ConfigValue::Int(3)),
]);
let value = ConfigValue::List(vec![good, bad]);

let err = validate(&schema, &value).unwrap_err();
assert_eq!(err.to_string(), "[1].file: expected string, got integer");