Skip to content

Commit

Permalink
Allow partial validation when using validate decorator (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
svisser authored and alecthomas committed Sep 9, 2017
1 parent f1fbe62 commit 48d1ea8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 48d1ea8

Please sign in to comment.