From 577cfae7c7835e89e592af7cd024d6e9de084ed1 Mon Sep 17 00:00:00 2001 From: Dustin Ingram Date: Fri, 25 Aug 2017 16:12:53 -0500 Subject: [PATCH] RangeValidator updates (#38) * Add failing test * RangeValidator should handle invalid values * Version 0.0.16 release --- setup.py | 2 +- vladiate/test/test_validators.py | 8 ++++++++ vladiate/validators.py | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 9913bb9..8de8422 100644 --- a/setup.py +++ b/setup.py @@ -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): diff --git a/vladiate/test/test_validators.py b/vladiate/test/test_validators.py index 513675f..66d431d 100644 --- a/vladiate/test/test_validators.py +++ b/vladiate/test/test_validators.py @@ -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("") diff --git a/vladiate/validators.py b/vladiate/validators.py index b6015e9..bbbb18c 100644 --- a/vladiate/validators.py +++ b/vladiate/validators.py @@ -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(