Skip to content

Warn about (forbid?) numeric validators with max_value but without min_value #101

Description

@binaryDiv

Numeric validators like the IntegerValidator or DecimalValidator currently have the parameters min_value and max_value, which both default to None, meaning there are no limits for the input numbers.

An easy mistake to make is to forget setting min_value when using max_value.

For example, an IntegerValidator(max_value=100) would only allow values smaller or equal to 100. However, this includes all negative numbers as well, so -9999 would be allowed.

I can't think of any usecase where this behaviour would be wanted. If you set max_value=100, you probably also want min_value=0 or maybe even min_value=-100.

To avoid this mistake (I've done it in multiple places, which is why I'm opening this ticket), I'd suggest to either raise a warning when a numeric validator is defined with max_value but without min_value, or even raise an error to forbid it completely.

If you really want a "-Infinity to 100" validator, you would have to set min_value=None explicitly (or rather min_value=IntegerValidator.DEFAULT_MIN_VALUE, since the default limit is not infinity).

This could be considered a breaking change as it technically changes the default behaviour of the min_value parameter. However, in all potentially affected cases, the code probably is wrong in the first place and should be fixed. :)

I'm not 100% sure about whether to raise a warning or an actual error. A warning wouldn't break existing code, so probably we should use a warning for now. (Maybe change it to a real error in version 1.0?)

NOTE: Setting min_value without max_value however should be fine! E.g. IntegerValidator(min_value=0) for only positive numbers makes total sense.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

breaking changesThis issue will cause a breaking change (or deprecation warning).bugSomething isn't working

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions