Last active
August 29, 2015 14:03
-
-
Save adoc/9614d9ece816dc5743d6 to your computer and use it in GitHub Desktop.
pyr_validate_view.py: Pyamid View Decorator for Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate_model(params=None, match=None, headers=lambda r: tuple(), | |
json=json, | |
invalid_params_exc=pyramid.httpexceptions.HTTPBadRequest, | |
invalid_match_exc=pyramid.httpexceptions.HTTPNotFound | |
): | |
"""Basic validation decorator for usage in `view_config`. | |
Takes `params` and `match` as arguments. | |
`params` - Schema to use to and instruct to validate requests.params | |
`match` - Schema to use to and isntruct to validate request.match | |
""" | |
if params is None and match is None: # Validate the usage of the validator! | |
raise ValueError("`validate_model` expected a `params` schema or a " | |
"`match` schema.") | |
# Check to see if Validator works as well. | |
if params and issubclass(params, (formencode.Schema, | |
formencode.FancyValidator)): | |
params = params() | |
elif params is not None: | |
raise ValueError("`params` expected a `formencode.Schema` type.") | |
if match and issubclass(match, (formencode.Schema, | |
formencode.FancyValidator)): | |
match = match() | |
elif match is not None: | |
raise ValueError("`match` expected a `formencode.Schema` type.") | |
def _decorator(view_callable): | |
def _inner(context, request): | |
def validate_params(this): | |
data = request.json_body or request.params | |
try: | |
data = params.to_python(data) | |
except formencode.Invalid as e: | |
logging.error("`validate_model` failed on request.params " | |
"%s. Error: %s" % (data, e.msg)) | |
body = json.dumps({'msg': e.unpack_errors()}) | |
raise invalid_params_exc(headers=headers(request), | |
body=body) | |
else: | |
return data | |
def validate_match(this): | |
try: | |
data = match.to_python(request.matchdict) | |
except formencode.Invalid as e: | |
logging.error("`validate_model` failed on request.matchdict" | |
" %s." % request.matchdict) | |
body = json.dumps({'msg': e.unpack_errors()}) | |
raise invalid_match_exc(headers=headers(request), | |
body=body) | |
else: | |
return data | |
if params: | |
request.set_property(validate_params, 'validated_params', | |
reify=True) | |
if match: | |
request.set_property(validate_match, 'validated_matchdict', | |
reify=True) | |
return view_callable(context, request) | |
return _inner | |
return _decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment