-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate correlated sibling fields #73
Comments
The For equality between two fields, we could add an #[derive(garde::Validate)]
struct User {
#[garde(length(min=1, max=255))]
password: String,
#[garde(equals(self.password))]
password2: String,
} |
Adding #[derive(garde::Validate)]
pub struct User {
#[garde(length(min = 1, max = 255))]
password: String,
#[garde(length(min = 1, max = 255))]
#[garde(custom(|value: &String, _ctx: &()| {
some_shared_validator(value, self.repeat_password)
}))]
repeat_password: String,
}
fn some_shared_validator(val: &String, other: &String) {} instead of #[derive(garde::Validate)]
pub struct User {
#[garde(length(min = 1, max = 255))]
password: String,
#[garde(length(min = 1, max = 255))]
#[garde(custom(some_shared_validator, other = self.repeat_password))]
repeat_password: String,
}
fn some_shared_validator(val: &String, other: &String, _ctx: &()) {} |
The general solution is #[derive(garde::Validate)]
struct User {
#[garde(length(min = 1, max = 255))]
password: String,
#[garde(custom(some_shared_validator(&self.password2)))]
password2: String,
}
fn some_shared_validator(other: &str) -> impl FnOnce(&str, &()) -> garde::Result {
|value, ctx| todo!()
} |
higher-order function works very well, I think this issue might be close but some extra example in README will be very welcomed. |
I added an example to the README in 5b80e50, but I'm keeping this open for an |
Perhaps I'm misunderstanding but is the |
It's quite common to have a logic in validation where we want to validate field based on values in other fields, or ensure that all fields are either filled or empty.
field_name
to theValidationError
). There would be super helpful to have an access togarde::Report
inside custom validator. We can usevalidate_into
but it's not working with#[derive(garde::Validate)]
self
values tocustom
validator like:self
:Option 3 looks like the best solution but I can't find any confirmation in README that it's supported and recommended way of accessing struct siblings.
The text was updated successfully, but these errors were encountered: