Skip to content

Commit

Permalink
Merge c8e71f3 into 53cd400
Browse files Browse the repository at this point in the history
  • Loading branch information
syxolk committed Dec 30, 2017
2 parents 53cd400 + c8e71f3 commit 5981a28
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions vladiate/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_set_validator_works(field_set, field):
([], 'bar'),
(['foo'], 'bar'),
(['foo', 'bar'], 'baz'),
([str(x) for x in range(200)], "notanumber"),
])
def test_set_validator_fails(field_set, field):
validator = SetValidator(field_set)
Expand Down
15 changes: 14 additions & 1 deletion vladiate/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from itertools import islice

from vladiate.exceptions import ValidationException, BadValidatorException

Expand Down Expand Up @@ -70,7 +71,8 @@ def validate(self, field, row={}):
if field not in self.valid_set:
self.invalid_set.add(field)
raise ValidationException(
"'{}' is not in {}".format(field, self.valid_set))
"'{}' is not in {}".format(field,
stringify_set(self.valid_set, 100)))

@property
def bad(self):
Expand Down Expand Up @@ -208,3 +210,14 @@ def validate(self, field, row={}):
@property
def bad(self):
pass


def stringify_set(a_set, max_len):
''' Stringify `max_len` elements of `a_set` and count the remainings '''
# Don't convert `a_set` to a list for performance reasons
text = "{{{}}}".format(", ".join(
"'{}'".format(value) for value in islice(a_set, max_len)
))
if len(a_set) > max_len:
text += " ({} more suppressed)".format(len(a_set) - max_len)
return text

0 comments on commit 5981a28

Please sign in to comment.