Skip to content
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

min must be lower than max #38

Closed
leontoeides opened this issue Apr 9, 2023 · 2 comments
Closed

min must be lower than max #38

leontoeides opened this issue Apr 9, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@leontoeides
Copy link

Thank you for this crate. I really appreciate the validator crate too.

I'm getting an error with this crate when a length's min and max are set to the same value.

I would like for this field to be exactly 2 characters, no more and no less.

If this is invalid, how would I express in this in garde without a custom rule? Or maybe this is an oversight?

Thank you for the crate and your help!

error: `min` must be lower than `max`
   |
34 |     #[garde(length(min = 2, max = 2))]
   |                    ^^^
@jprochazk
Copy link
Owner

jprochazk commented Apr 9, 2023

This is definitely a bug. It should be possible to set min and max to the same value, because the comparison ops used are < and >:

if len < min {
Err(InvalidLength::Min)
} else if len > max {
Err(InvalidLength::Max)
} else {
Ok(())
}

For the time being, you can use a custom validator which just wraps rules::length:

#[derive(garde::Validate)]
struct Test {
    //                                                  min  max
    //                                                    v  v
    #[garde(custom(|v, _| garde::rules::length::apply(v, (2, 2))))]
    field: String,
}

// or as a reusable one:
fn len_eq_2<T: garde::rules::length::Length>(v: &T, _: &()) -> garde::Result {
    garde::rules::length::apply(v, (2, 2))
}

#[derive(Debug, garde::Validate)]
struct Test2 {
    #[garde(custom(len_eq_2))]
    field: String,
}

@jprochazk jprochazk added the bug Something isn't working label Apr 9, 2023
@jprochazk
Copy link
Owner

...or update to 0.11.2 🙂. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants