From 2a00bc443336ffe4b8174152e92b17aa8e5dc991 Mon Sep 17 00:00:00 2001 From: Nicko van Someren Date: Sun, 10 Feb 2019 09:23:25 -0700 Subject: [PATCH 1/3] Added support for default values in Inclusive schemas --- voluptuous/schema_builder.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index c98f20d..4bbe70f 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, default=UNDEFINED, description=None): super(Inclusive, self).__init__(schema, msg=msg, + default=default, description=description) self.group_of_inclusion = group_of_inclusion From 1e23b2dbd51e3453b00baa6367e52494567c23bb Mon Sep 17 00:00:00 2001 From: Nicko van Someren Date: Tue, 12 Feb 2019 10:50:25 -0700 Subject: [PATCH 2/3] Added new test cases for Inclusive and Exclusive schema entries. --- voluptuous/tests/tests.py | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) 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" From d6e83a6b4aa29e1c18361a0057b5fdae9b00f251 Mon Sep 17 00:00:00 2001 From: Nicko van Someren Date: Sat, 16 Feb 2019 07:17:32 -0700 Subject: [PATCH 3/3] Moved additional parameter to the end of the arguments list. --- voluptuous/schema_builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index 4bbe70f..89745b6 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -1128,7 +1128,7 @@ class Inclusive(Optional): """ def __init__(self, schema, group_of_inclusion, - msg=None, default=UNDEFINED, description=None): + msg=None, description=None, default=UNDEFINED): super(Inclusive, self).__init__(schema, msg=msg, default=default, description=description)