Merge pull request #7 from microsoft/grecoe/doc
Added a mixed function parameter type between predefined args WITH kw…
This commit is contained in:
Коммит
2ca014b67a
|
@ -8,7 +8,37 @@ This repo has a decorator class that can be used in both cases.
|
|||
## Usage
|
||||
To use this functionality, you need to use the validation_decorator.ParameterValidator to decorate your methods.
|
||||
|
||||
Regardless of method type (standalone or class) you can use one of two options
|
||||
Regardless of method type there are multiple options, with examples:
|
||||
|
||||
1. A function that has all predefined arguments
|
||||
```python
|
||||
"""
|
||||
Have to define an entry for each incoming parameter, in this case 2 ints
|
||||
that cannot be None
|
||||
"""
|
||||
@ParameterValidator((int, False), (int, False))
|
||||
def myfunc(param1, param2)
|
||||
```
|
||||
2. A function that has only kwargs
|
||||
```python
|
||||
"""
|
||||
Define only the arguments you expect. In this case one int (count) and
|
||||
one string (name) where neither can be None.
|
||||
|
||||
If passed True (can be None) no error is thrown if the field is not in kwargs.
|
||||
"""
|
||||
@ParameterValidator(count=(int, False), name=(str, False))
|
||||
def myfunc(**kwwargs)
|
||||
```
|
||||
3. A function with mixed predefined args and kwargs
|
||||
```python
|
||||
"""
|
||||
A mix of above, for all predefined arguments you MUST have an entry, but
|
||||
for kwargs define only what you want to verify.
|
||||
"""
|
||||
@ParameterValidator((int, False), name=(str, False))
|
||||
def myfunc(count, **kwwargs)
|
||||
```
|
||||
|
||||
### Validation Tuples
|
||||
In all cases there is a validation tuple defined as
|
||||
|
|
|
@ -96,13 +96,24 @@ class ParameterValidator:
|
|||
if len(args_to_validate) and (len(args) != func.__code__.co_argcount):
|
||||
raise ParameterCountValidationException(func, len(args))
|
||||
|
||||
# We may have args and kwargs so track if we are checking kwargs
|
||||
validate_kwargs = False
|
||||
if len(args_to_validate):
|
||||
validate_kwargs = len(self.validation_kwargs) > 0
|
||||
self._validate_args_arguments(func, args_to_validate, self.validation_args)
|
||||
elif len(kwargs) and len(self.validation_kwargs):
|
||||
self._validate_kwargs_arguments(func, kwargs, self.validation_kwargs)
|
||||
elif len(self.validation_kwargs):
|
||||
validate_kwargs = True
|
||||
else:
|
||||
raise ParameterCountValidationException(func, 0)
|
||||
|
||||
# Check kwargs?
|
||||
if validate_kwargs:
|
||||
# If no kwargs passed we still have to validate because there may
|
||||
# be required kwargs parameters.
|
||||
if not kwargs:
|
||||
kwargs = {}
|
||||
self._validate_kwargs_arguments(func, kwargs, self.validation_kwargs)
|
||||
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ from paramvalidator.exceptions import (
|
|||
)
|
||||
|
||||
|
||||
def test_predefine_params():
|
||||
def test_predefine_params_args():
|
||||
"""
|
||||
In this case our method pre-defines all of it's parameters, i.e.
|
||||
|
||||
|
@ -51,6 +51,15 @@ def test_predefine_params():
|
|||
myfunc(*single_args)
|
||||
|
||||
|
||||
# Splatting - Too many arguments
|
||||
single_args.append("One too many")
|
||||
try:
|
||||
print("Standalone Args Splatting - success")
|
||||
myfunc(*single_args)
|
||||
except ParameterValidationException as ex:
|
||||
assert(isinstance(ex, ParameterCountValidationException))
|
||||
print("\t",str(ex))
|
||||
|
||||
# Standard call but make first parameter None where it's not allowed
|
||||
try:
|
||||
print("Standalone Args Standard - failure on invalid type for parameter")
|
||||
|
@ -74,7 +83,7 @@ def test_predefine_params():
|
|||
assert(isinstance(ex, ParameterNoneValidationException))
|
||||
print("\t",str(ex))
|
||||
|
||||
def test_predfined_params_2():
|
||||
def test_predfined_params_kwargs():
|
||||
"""
|
||||
In this case our method pre-defines all of it's parameters, i.e.
|
||||
|
||||
|
@ -104,3 +113,30 @@ def test_predfined_params_2():
|
|||
assert(isinstance(ex, ParameterKwargValidationException))
|
||||
print("\t",str(ex))
|
||||
|
||||
try:
|
||||
print("Standalone Kwargs Standard - failure on missing required param 2")
|
||||
mykwfunc()
|
||||
except ParameterValidationException as ex:
|
||||
print("\t",type(ex), str(ex))
|
||||
assert(
|
||||
isinstance(ex, ParameterKwargValidationException)
|
||||
or
|
||||
isinstance(ex, ParameterCountValidationException)
|
||||
)
|
||||
|
||||
def test_params_mixed():
|
||||
|
||||
@ParameterValidator((int, False), name=(str,False))
|
||||
def mymixedfunc(num, **kwargs):
|
||||
print("Hello from standalone function")
|
||||
|
||||
print("Standalone Mixed - ok")
|
||||
mymixedfunc(3, name="Name")
|
||||
|
||||
try:
|
||||
print("Standalone Mixed - failure on missing required param")
|
||||
mymixedfunc(3)
|
||||
except ParameterValidationException as ex:
|
||||
assert(isinstance(ex, ParameterKwargValidationException))
|
||||
print("\t",str(ex))
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче