Commit fb637a21 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

initial validators working

parent 0f3a3792
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<?eclipse-pydev version="1.0"?> <?eclipse-pydev version="1.0"?>
<pydev_project> <pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">pypy</pydev_property> <pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">python</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property> <pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH"> <pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/corepost</path> <path>/corepost</path>
......
#Sat Sep 03 22:26:56 EDT 2011
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
...@@ -18,10 +18,10 @@ Feature: Argument Validators ...@@ -18,10 +18,10 @@ Feature: Argument Validators
| custom | childId=jacekf | 201 | 23 - jacekf - {} | | custom | childId=jacekf | 201 | 23 - jacekf - {} |
| custom | childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} | | custom | childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} |
| custom | childId=test | 201 | 23 - test - {} | | custom | childId=test | 201 | 23 - test - {} |
| custom | childId=wrong | 400 | 'wrong' is not a valid value for 'childId': The input is not valid | | custom | childId=wrong | 400 | childId: The input is not valid ('wrong') |
# validates using Schema # validates using Schema
| schema | childId=jacekf | 201 | 23 - jacekf - {} | | schema | childId=jacekf | 201 | 23 - jacekf - {} |
| schema | childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} | | schema | childId=jacekf&otherId=test | 201 | 23 - jacekf - {'otherId': 'test'} |
| schema | childId=test | 201 | 23 - test - {} | | schema | childId=test | 201 | 23 - test - {} |
| schema | childId=wrong | 400 | 'wrong' is not a valid value for 'childId': The input is not valid | | schema | childId=wrong | 400 | childId: The input is not valid ('wrong') |
\ No newline at end of file
...@@ -251,16 +251,15 @@ def validate(schema=None,**vKwargs): ...@@ -251,16 +251,15 @@ def validate(schema=None,**vKwargs):
def fn(realfn): def fn(realfn):
def wrap(*args,**kwargs): def wrap(*args,**kwargs):
# first run schema validation, then the custom validators # first run schema validation, then the custom validators
errors = () errors = []
if schema != None: if schema != None:
try: try:
schema.to_python(kwargs) schema.to_python(kwargs)
except Invalid as ex: except Invalid as ex:
errors = () for arg, error in ex.error_dict.items():
for error in ex.error_dict.keys(): errors.append("%s: %s ('%s')" % (arg,error.msg,error.value))
errors.append()
raise TypeError("%s" % ex)
# custom validators
for arg in vKwargs.keys(): for arg in vKwargs.keys():
validator = vKwargs[arg] validator = vKwargs[arg]
if arg in kwargs: if arg in kwargs:
...@@ -268,10 +267,15 @@ def validate(schema=None,**vKwargs): ...@@ -268,10 +267,15 @@ def validate(schema=None,**vKwargs):
try: try:
validator.to_python(val) validator.to_python(val)
except Invalid as ex: except Invalid as ex:
raise TypeError("'%s' is not a valid value for '%s': %s" % (val,arg,ex)) errors.append("%s: %s ('%s')" % (arg,ex,val))
else: else:
if isinstance(validator,FancyValidator) and validator.not_empty: if isinstance(validator,FancyValidator) and validator.not_empty:
raise TypeError("Missing mandatory argument '%s'" % arg) raise TypeError("Missing mandatory argument '%s'" % arg)
# fire error if anything failed validation
if len(errors) > 0:
raise TypeError('\n'.join(errors))
# all OK
return realfn(*args,**kwargs) return realfn(*args,**kwargs)
return wrap return wrap
return fn return fn
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment