Skip to content

between doesn't work with the number 0 #298

Closed

Description

@scotttyso
>>> import validators
>>> validators.between(0, min_val=0, max_val=5)
ValidationError(func=between, args={'value': 0, 'min_val': 0, 'max_val': 5})
>>> validators.between(1, min_val=0, max_val=5)
True
>>> validators.between(0, min_val=-1, max_val=5)
ValidationError(func=between, args={'value': 0, 'min_val': -1, 'max_val': 5})
>>> validators.between(-1, min_val=-1, max_val=5)
True
>>> 

Activity

  1. scotttyso commented on Sep 14, 2023

    @scotttyso
    Author

    For now I downgraded to validators to 0.11.0, which seems to be an old/different version of the module to work around this issue.

  2. nandgator commented on Sep 17, 2023

    @nandgator
    Collaborator

    Nice catch! Here's a patch:

    diff --git a/src/validators/between.py b/src/validators/between.py
    index 77c9ae3..2fc3a0c 100644
    --- a/src/validators/between.py
    +++ b/src/validators/between.py
    @@ -8,7 +8,7 @@ from typing import TypeVar, Union
     from ._extremes import AbsMax, AbsMin
     from .utils import validator
     
    -PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime)
    +PossibleValueTypes = TypeVar("PossibleValueTypes", int, float, str, datetime, None)
     
     
     @validator
    @@ -67,7 +67,7 @@ def between(
     
         > *New in version 0.2.0*.
         """
    -    if not value:
    +    if value is None:
             return False
     
         if min_val is max_val is None:
    diff --git a/tests/test_between.py b/tests/test_between.py
    index 8e59ad6..6b1ccd7 100644
    --- a/tests/test_between.py
    +++ b/tests/test_between.py
    @@ -15,7 +15,7 @@ T = TypeVar("T", int, float, str, datetime)
     
     @pytest.mark.parametrize(
         ("value", "min_val", "max_val"),
    -    [(12, 11, 13), (12, None, 14), (12, 11, None), (12, 12, 12)],
    +    [(12, 11, 13), (12, None, 14), (12, 11, None), (12, 12, 12), (0, 0, 0), (0, -1, 3)],
     )
     def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T):
         """Test returns true on valid range."""
    @@ -25,6 +25,7 @@ def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T):
     @pytest.mark.parametrize(
         ("value", "min_val", "max_val"),
         [
    +        (None, 13, 14),
             (12, 13, 14),
             (12, None, 11),
             (12, None, None),

    PR is welcome!

  3. added 2 commits that reference this issue on Sep 23, 2023
    848b7dd
    f7aa593
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue: Works not as designed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions