From 12fcff7e4ad6f480d407428adcdf4fc9ba1270f4 Mon Sep 17 00:00:00 2001 From: Simeon Visser Date: Fri, 8 Sep 2017 11:10:03 +0100 Subject: [PATCH] Allow partial validation when using validate decorator (#296) --- voluptuous/schema_builder.py | 3 ++- voluptuous/tests/tests.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index 6e14138..429ab32 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -1171,7 +1171,8 @@ def validate_schema_decorator(func): returns = schema_arguments[RETURNS_KEY] del schema_arguments[RETURNS_KEY] - input_schema = Schema(schema_arguments) if len(schema_arguments) != 0 else lambda x: x + input_schema = (Schema(schema_arguments, extra=ALLOW_EXTRA) + if len(schema_arguments) != 0 else lambda x: x) output_schema = Schema(returns) if returns_defined else lambda x: x @wraps(func) diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index e3ab827..8b82f98 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -680,6 +680,38 @@ def fn(arg): assert_raises(Invalid, fn, 1) +def test_schema_decorator_partial_match_called_with_args(): + @validate(arg1=int) + def fn(arg1, arg2): + return arg1 + + fn(1, "foo") + + +def test_schema_decorator_partial_unmatch_called_with_args(): + @validate(arg1=int) + def fn(arg1, arg2): + return arg1 + + assert_raises(Invalid, fn, "bar", "foo") + + +def test_schema_decorator_partial_match_called_with_kwargs(): + @validate(arg2=int) + def fn(arg1, arg2): + return arg1 + + fn(arg1="foo", arg2=1) + + +def test_schema_decorator_partial_unmatch_called_with_kwargs(): + @validate(arg2=int) + def fn(arg1, arg2): + return arg1 + + assert_raises(Invalid, fn, arg1=1, arg2="foo") + + def test_unicode_as_key(): if sys.version_info >= (3,): text_type = str