Skip to content

Commit

Permalink
RangeValidator updates (#38)
Browse files Browse the repository at this point in the history
* Add failing test

* RangeValidator should handle invalid values

* Version 0.0.16 release
  • Loading branch information
di authored Aug 25, 2017
1 parent efb9da6 commit 577cfae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand

__version__ = '0.0.15'
__version__ = '0.0.16'


class PyTest(TestCommand):
Expand Down
8 changes: 8 additions & 0 deletions vladiate/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ def test_range_validator_fails():
assert validator.bad == {'-42'}


def test_range_validator_handles_bad_values():
validator = RangeValidator(0, 100)
with pytest.raises(ValidationException):
validator.validate("foobar")

assert validator.bad == {'foobar'}


def test_empty_validator_works():
EmptyValidator().validate("")

Expand Down
6 changes: 5 additions & 1 deletion vladiate/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ def __init__(self, low, high):
self.outside = set()

def validate(self, field, row={}):
if not self.low <= float(field) <= self.high:
try:
value = float(field)
if not self.low <= value <= self.high:
raise ValueError
except ValueError:
self.outside.add(field)
raise ValidationException(
"'{}' is not in range {} to {}".format(
Expand Down

0 comments on commit 577cfae

Please sign in to comment.