Skip to content

Nested structure errors #40

@axos88

Description

@axos88

Hey, nice crate, was thinking about something similar in ya7010/serde_valid#106, but just stumbled upon your solution.

You seem to have a similar issue with nested structure errors:

#[derive(Validate, Deserialize, Debug)]
#[serde(validate = "serde_valid::Validate::validate")]
struct Foo {
  #[validate(multiple_of = 2)] even: i32,
  #[validate(pattern= r"^[^@\s]+@[^@\s]+\.[^@\s]+$")] email: String,
  #[validate(minimum = 0)] #[validate(maximum = 100)] percentage: u8
}

#[derive(Validate, Deserialize, Debug)]
#[serde(validate = "serde_valid::Validate::validate")]
struct Bar { foo: Foo }
  let json_invalid = r#"
{
    "foo": 
        {
            "even": 5,
            "email": "bad_email",
            "percentage": 125
        }
}
"#;

  println!("JSON invalid reslt: {:?}", serde_json::from_str::<Bar>(json_invalid).err().unwrap());

Reports:

{
  "errors": [],
  "properties": {
    "even": {
      "errors": [
        "The value must be multiple of `2`."
      ]
    },
    "email": {
      "errors": [
        "The value must match the pattern of ..."
      ]
    },
    "percentage": {
      "errors": [
        "The number must be `<= 100`."
      ]
    }
  }
}

If this was a deeply nested struct (or an array), it would cause some issue finding which path caused the issue. It would be great if there could be visibility on which nested struct causes the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions