From e26d82dfb818bb931babbffc0e33776f4b969501 Mon Sep 17 00:00:00 2001 From: Dominik Kellner Date: Mon, 28 Sep 2015 16:39:53 +0200 Subject: [PATCH] Prepend field name to ValidationError messages --- booby/models.py | 8 ++++++-- tests/integration/test_model.py | 6 +++--- tests/unit/models/test_model.py | 10 ++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/booby/models.py b/booby/models.py index 9a22ab0..5e5cc74 100644 --- a/booby/models.py +++ b/booby/models.py @@ -174,12 +174,16 @@ def validate(self): all the :mod:`fields` within this model. If some `field` validation fails, then this method raises the same - exception that the :func:`field.validate` method had raised. + exception that the :func:`field.validate` method had raised, but + with the field name prepended. """ for name, field in self._fields.items(): - field.validate(getattr(self, name)) + try: + field.validate(getattr(self, name)) + except errors.ValidationError as err: + raise errors.ValidationError('%s %s' % (name, err)) @property def validation_errors(self): diff --git a/tests/integration/test_model.py b/tests/integration/test_model.py index 93329c6..322bd13 100644 --- a/tests/integration/test_model.py +++ b/tests/integration/test_model.py @@ -26,7 +26,7 @@ def callback(): user.validate() expect(callback).to(raise_error(errors.ValidationError, - 'should be an integer')) + 'karma should be an integer')) def test_should_fail_validation_if_token_key_is_not_a_string(self): def callback(): @@ -34,7 +34,7 @@ def callback(): user.validate() expect(callback).to(raise_error(errors.ValidationError, - 'should be a string')) + 'token key should be a string')) def test_should_fail_validation_if_invalid_email(self): def callback(): @@ -42,7 +42,7 @@ def callback(): user.validate() expect(callback).to(raise_error(errors.ValidationError, - 'should be a valid email')) + 'email should be a valid email')) class TestEncode(object): diff --git a/tests/unit/models/test_model.py b/tests/unit/models/test_model.py index a7b2ad9..9499bd1 100644 --- a/tests/unit/models/test_model.py +++ b/tests/unit/models/test_model.py @@ -52,7 +52,7 @@ def test_when_validate_and_validation_errors_then_raises_first_validation_error( user = UserWithRequiredName(email='foo@example.com') expect(lambda: user.validate()).to(raise_error( - errors.ValidationError, 'is required')) + errors.ValidationError, 'name is required')) def test_when_validate_without_errors_then_does_not_raise(self): user = UserWithRequiredName(name='Jack') @@ -85,6 +85,12 @@ def test_when_validation_errors_and_no_errors_then_returns_none(self): expect(errors).to(be_empty) + def test_exceptions_contain_field_names(self): + user = UserWithRequiredName() + + expect(user.validate).to(raise_error(errors.ValidationError, + 'name is required')) + class TestInheritedModel(object): def test_when_pass_kwargs_then_set_fields_values(self): @@ -123,7 +129,7 @@ class User(UserMixin, models.Model): user = User() expect(lambda: user.validate()).to(raise_error( - errors.ValidationError, 'is required')) + errors.ValidationError, 'name is required')) class TestDictModel(object):