diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index c98f20d..89745b6 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -1127,8 +1127,10 @@ class Inclusive(Optional): True """ - def __init__(self, schema, group_of_inclusion, msg=None, description=None): + def __init__(self, schema, group_of_inclusion, + msg=None, description=None, default=UNDEFINED): super(Inclusive, self).__init__(schema, msg=msg, + default=default, description=description) self.group_of_inclusion = group_of_inclusion diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index f2db8da..1c144eb 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -1349,3 +1349,60 @@ def test_any_required_with_subschema(): "required key not provided @ data['a']") else: assert False, "Did not raise Invalid for MultipleInvalid" + +def test_inclusive(): + schema = Schema({ + Inclusive('x', 'stuff'): int, + Inclusive('y', 'stuff'): int, + }) + + r = schema({}) + assert_equal(r, {}) + + r = schema({'x':1, 'y':2}) + assert_equal(r, {'x':1, 'y':2}) + + try: + r = schema({'x':1}) + except MultipleInvalid as e: + assert_equal(str(e), + "some but not all values in the same group of inclusion 'stuff' @ data[]") + else: + assert False, "Did not raise Invalid for incomplete Inclusive group" + +def test_inclusive_defaults(): + schema = Schema({ + Inclusive('x', 'stuff', default=3): int, + Inclusive('y', 'stuff', default=4): int, + }) + + r = schema({}) + assert_equal(r, {'x':3, 'y':4}) + + try: + r = schema({'x':1}) + except MultipleInvalid as e: + assert_equal(str(e), + "some but not all values in the same group of inclusion 'stuff' @ data[]") + else: + assert False, "Did not raise Invalid for incomplete Inclusive group with defaults" + +def test_exclusive(): + schema = Schema({ + Exclusive('x', 'stuff'): int, + Exclusive('y', 'stuff'): int, + }) + + r = schema({}) + assert_equal(r, {}) + + r = schema({'x':1}) + assert_equal(r, {'x':1}) + + try: + r = schema({'x':1, 'y': 2}) + except MultipleInvalid as e: + assert_equal(str(e), + "two or more values in the same group of exclusion 'stuff' @ data[]") + else: + assert False, "Did not raise Invalid for multiple values in Exclusive group"