-
Notifications
You must be signed in to change notification settings - Fork 144
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
Feature: Add exclusive_min
and exclusive_max
to range
validation
#246
Feature: Add exclusive_min
and exclusive_max
to range
validation
#246
Conversation
exc_min
and exc_max
to range
validationexc_min
and exc_max
to range
validation
I can see the point of half open/closed interval but I've never seen excluding the first value of the range. When is that useful? |
Do you mean having something along the lines of |
How do you represent it with the Rust range syntax? I was thinking of just using that for the next version to represent the range (eg instead of |
Okay, I just checked, and it definitely seems like Rust's Let me think about it... although I have a feeling that we should keep the |
Hi! I do believe, though, that using Rust's The reasoning behind this is that, well, things become clunky once you want to represent things such as: #[validate(range(max = 123.0))] because you'd have to do something like: #[validate(range(-f64::INFINITY..=123.0))] And that is definitely longer, and clearly not that humanly readable. I'd say clearly a downgrade. Doing a So, what I would do, is to keep the What do you think? |
I would rather use the API from https://marshmallow.readthedocs.io/en/stable/marshmallow.validate.html#marshmallow.validate.Range where you have min_inclusive/max_inclusive as bool to keep min/max. |
I am not that big of a fan... I feel that it possibly exists because of the positional arguments So, if applying the #[validate(range(min = -180.0, max = 180.0, exclusive_min = true, exclusive_max = true)] And with the proposed system in this PR, it would look like this: #[validate(range(exc_min = -180.0, exc_max = 180.0))] And honestly, I'd rather go with the later, but now that I'm thinking, this following one looks the best now to me: #[validate(range(exclusive_min = -180.0, exclusive_max = 180.0))] It feels a lot clearer than the |
For what it's worth, I asked ChatGPT and it agrees that the 3rd option is better 😅 |
Nevermind they are numbers in openapi 3.1 |
Yep, I was basing myself mostly on the latest JSON Schema validation specification: |
Can you add the full name (exclusive) and do it against #249 ? |
exc_min
and exc_max
to range
validationexclusive_min
and exclusive_max
to range
validation
There it goes! 😃 |
validator_derive/src/validation.rs
Outdated
FieldValidation { | ||
message, | ||
code: code.unwrap_or_else(|| validator.code().to_string()), | ||
validator, | ||
} | ||
} | ||
|
||
fn collide<T>(first: &Option<T>, second: &Option<T>) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's easier to read without this function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll remove it this weekend. |
Thanks! |
You are welcome! It was a blast. Thank you for merging. Looking forward to the |
#246) * feat(range): add exclusive minimum and exclusive maximum for `range` validation * test(range): add tests for `exc_min` and `exc_max` range validation * docs: add docs for `exc_min` and `exc_max` for `range` validation * chore: rename `exc_min`, `exc_max` to `exclusive_min`, `exclusive_max` * chore(validation.rs): get rid of `collide` function
#246) * feat(range): add exclusive minimum and exclusive maximum for `range` validation * test(range): add tests for `exc_min` and `exc_max` range validation * docs: add docs for `exc_min` and `exc_max` for `range` validation * chore: rename `exc_min`, `exc_max` to `exclusive_min`, `exclusive_max` * chore(validation.rs): get rid of `collide` function
Description
This pull request adds the ability to introduce
exclusive_min
andexclusive_max
parameters (temporarily namedexc_min
andexc_max
) to therange
validation. With this new feature, users can specify whether the range should exclude the minimum and/or maximum values.For example, if a user wants to validate that a height field is within the range of 0.0 (exclusive) to 100.0 (inclusive), they can now use the following code:
This situation is mostly relevant to non-integers, but I added the feature to support all the currently supported types because... who am I to stop people from using this feature with integers? Sometimes, it even makes sense (your limit is 299 but it is cleaner to say
exclusive_max = 300
).It also forbids from inserting both inclusive and exclusive "minimums" or "maximums", so this is not possible:
Motivation
Currently, the
range
validation in this crate only allows users to specify an inclusive range. However, there are cases where users may want to exclude one or both of the endpoints of the range.I come from a Python background, and
pydantic
is an well known validation library that has this feature, which was probably the main motivation for me to do this, as I have had to use this kind of exclusive limitation in some particular cases.Another reason why I decided to do this was to make it possible for developers of
schemars
to automate the JSON Schema generation of these kind of exclusive limits.Changes
Aside from the feature itself, tests and documentation have also been updated to reflect the new arguments and their limitations.
If there is any other thing that should be added in this Pull Request, please point it out, and I will try and do it.