Skip to content

Commit

Permalink
Prepend field name to ValidationError messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dkellner committed Sep 28, 2015
1 parent 9a75c4e commit e26d82d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
8 changes: 6 additions & 2 deletions booby/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ 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():
user = User(login='root', token=Token(key=1))
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():
user = User(login='root', email='@localhost')
user.validate()

expect(callback).to(raise_error(errors.ValidationError,
'should be a valid email'))
'email should be a valid email'))


class TestEncode(object):
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/models/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_when_validate_and_validation_errors_then_raises_first_validation_error(
user = UserWithRequiredName(email='[email protected]')

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')
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e26d82d

Please sign in to comment.