Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contains validation for the scheme validator #258

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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