Keats / validator

Simple validation for Rust structs
MIT License
1.91k stars 140 forks source link

`required` and `must_match` are incompatible #330

Open jaw-sh opened 1 month ago

jaw-sh commented 1 month ago

Version 0.18.1

Minimum demonstration.

#[derive(Debug, Deserialize, Default, Validate)]
pub struct XForm {
    #[validate(required)]
    pub a: Option<String>,
    #[validate(required, must_match(other = "a"))]
    pub b: Option<String>,
}

Error.

mismatched types
expected reference `&&std::string::String`
   found reference `&std::option::Option<std::string::String>`

must_match.rs(4, 8): function defined here

Removing required fixes this.

I have tried some cheeky workarounds, like removing required on just the second field, but that doesn't work. For instance, some combination of making one required and the other not required does not work. The following presents the same error.

#[derive(Debug, Deserialize, Default, Validate)]
pub struct XForm {
    #[validate(required, must_match(other = "b"))]
    pub a: Option<String>,
    pub b: Option<String>,
}

Edit: It actually appears that Options are totally incompatible, despite Option<T: Eq> generally working. This produces the same error.


#[derive(Debug, Deserialize, Default, Validate)]
pub struct XForm {
    #[validate(must_match(other = "b"))]
    pub a: Option<String>,
    pub b: Option<String>,
}