Skip to content

Commit

Permalink
added contains validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ziky90 committed Jan 12, 2017
1 parent 3e1144b commit b23fe91
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions voluptuous/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class ValueInvalid(Invalid):
"""The value was found invalid by evaluation function."""


class ContainsInvalid(Invalid):
"""List does not contain item"""


class ScalarInvalid(Invalid):
"""Scalars did not match."""

Expand Down
14 changes: 13 additions & 1 deletion voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Schema, Required, Optional, Extra, Invalid, In, Remove, Literal,
Url, MultipleInvalid, LiteralInvalid, NotIn, Match, Email,
Replace, Range, Coerce, All, Any, Length, FqdnUrl, ALLOW_EXTRA, PREVENT_EXTRA,
validate, ExactSequence, Equal, Unordered, Number, Maybe, Datetime, Date
validate, ExactSequence, Equal, Unordered, Number, Maybe, Datetime, Date,
Contains
)
from voluptuous.humanize import humanize_error
from voluptuous.util import to_utf8_py2, u
Expand Down Expand Up @@ -73,6 +74,17 @@ def test_not_in():
assert False, "Did not raise NotInInvalid"


def test_contains():
"""Verify contains validation method."""
schema = Schema({'color': Contains('red')})
schema({'color': ['blue', 'red', 'yellow']})
try:
schema({'color': ['blue', 'yellow']})
except Invalid as e:
assert_equal(str(e),
"value is not allowed for dictionary value @ data['color']")


def test_remove():
"""Verify that Remove works."""
# remove dict keys
Expand Down
24 changes: 22 additions & 2 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid, AnyInvalid,
AllInvalid, MatchInvalid, UrlInvalid, EmailInvalid, FileInvalid, DirInvalid, RangeInvalid,
PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, DateInvalid, InInvalid,
TypeInvalid, NotInInvalid)
TypeInvalid, NotInInvalid, ContainsInvalid)
except ImportError:
from .schema_builder import Schema, raises, message
from .error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid, AnyInvalid,
AllInvalid, MatchInvalid, UrlInvalid, EmailInvalid, FileInvalid, DirInvalid, RangeInvalid,
PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, DateInvalid, InInvalid,
TypeInvalid, NotInInvalid)
TypeInvalid, NotInInvalid, ContainsInvalid)


if sys.version_info >= (3,):
Expand Down Expand Up @@ -680,6 +680,26 @@ def __repr__(self):
return 'NotIn(%s)' % (self.container,)


class Contains(object):
"""Validate that a value is in a collection."""

def __init__(self, item, msg=None):
self.item = item
self.msg = msg

def __call__(self, v):
try:
check = self.item not in v
except TypeError:
check = True
if check:
raise ContainsInvalid(self.msg or 'value is not allowed')
return v

def __repr__(self):
return 'Contains(%s)' % (self.item, )


class ExactSequence(object):
"""Matches each element in a sequence against the corresponding element in
the validators.
Expand Down

0 comments on commit b23fe91

Please sign in to comment.