Skip to content
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

Allow partial validation when using validate decorator (#296) #303

Merged
merged 1 commit into from
Sep 9, 2017
Merged
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
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