Skip to content

Add bitwise operators to Schema class #48

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def __init__(self, schema, error=None):
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._schema)

def __and__(self, other):
return And(self, other)

def __or__(self, other):
return Or(self, other)

def validate(self, data):
s = self._schema
e = self._error
Expand Down
12 changes: 12 additions & 0 deletions test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ def test_and():
with SE: And(int, lambda n: 0 < n < 5).validate(3.33)
assert And(Use(int), lambda n: 0 < n < 5).validate(3.33) == 3
with SE: And(Use(int), lambda n: 0 < n < 5).validate('3.33')
# Bitwise operators:
s = Schema(int) & Schema(lambda n: 0 < n < 5)
assert s.validate(3) == 3
with SE: s.validate(3.33)
s = Schema(Use(int)) & Schema(lambda n: 0 < n < 5)
assert s.validate(3.33) == 3
with SE: s.validate('3.33')


def test_or():
Expand All @@ -68,6 +75,11 @@ def test_or():
with SE: Or(int, dict).validate('hai')
assert Or(int).validate(4)
with SE: Or().validate(2)
# Bitwise operators:
s = Schema(int) | Schema(dict)
assert s.validate(5) == 5
assert s.validate({}) == {}
with SE: s.validate('hai')


def test_validate_list():
Expand Down