jplatte / serde_html_form

Rust crate for (de-)serialization of `application/x-www-form-urlencoded` data.
MIT License
23 stars 3 forks source link

Access to original form data #16

Closed krazyuniks closed 7 months ago

krazyuniks commented 7 months ago

Hi,

I'm using this crate by way of axum_extra::extract. The deserialization fails if an html form is submitted with the wrong type, say age=twenty. This causes the extractor to fail and we never get the original form data so we can show the user the errors. What is the pattern to handle this? Currently I am maintaining one extractor form where all fields are Option String, so the form can be extracted regardless of input, and then building a second validation struct with the real field types to maintain the original submitted data and build error messages. This works but is not elegant. Any advice on the approach welcome.

#[derive(Debug, PartialEq, Deserialize)]
struct Form {
    one: u8,
    two: u8,
}

fn main() {
    match serde_html_form::from_str::<Form>("one=1&two=to&three=3") {
        Ok(parsed) => {
            assert_eq!(parsed, Form { one: 1, two: 2 });
        }
        Err(e) => {
            println!("Error: {}", e);
            // --> Error: invalid digit found in string
            // where is the rest?
        }
    };
}
jplatte commented 7 months ago

This is an axum-specific question, please move to axum's Q&A discussion section on GitHub.