jprochazk / garde

A powerful validation library for Rust
Apache License 2.0
476 stars 28 forks source link

Get access to Path components #76

Open kamilglod opened 1 year ago

kamilglod commented 1 year ago

I'm trying to use Path to build field path like ["top_level", "0", "nested_field1"], it would be easy when having access to Path.components but it's private. Can you add a possibility to access path in a different way than .to_string()? All that needs to be done is to make __iter() public.

jprochazk commented 12 months ago

Please post a code example which shows what you're trying to do. How does __iter help you build a path?

kamilglod commented 12 months ago

@jprochazk Let's say I have struct like:

pub struct ValidationError {
    pub field: Vec<String>,
    pub code: String,
}

where field is a filed like ["top_level", "0", "nested_field1"] And I want to return vec of those ValudationError's:

pub fn aggregate_errors(validation_errors: &Report) -> Vec<ValidationError> {
    let mut flatten_errors: Vec<ValidationError> = Vec::new();

    for (path, error) in validation_errors.iter() {
        let field = Vec::from_iter(path.__iter().rev().map(|(_kind, val)| val.to_string()));
        flatten_errors.push(ValidationError {
            code: error.message().to_string(),
            field,
        });
    }

    flatten_errors
}
jprochazk commented 12 months ago

Report is pretty much the same thing as a Vec<ValidationError>. What is ValidationError used for?

kamilglod commented 12 months ago

It's used to pass it as dict to Python code.