def require_schema(schema):
"""This decorator verifies that request JSON matches given JSONSchema.
http://json-schema.org
"""
validator = jsonschema.Draft4Validator(
schema,
format_checker=jsonschema.FormatChecker()
)
def outer_decorator(func):
@functools.wraps(func)
def inner_decorator(self, *args, **kwargs):
errors = validator.iter_errors(self.request_json)
errors = [err.message for err in errors]
if errors:
LOG.warning("Cannot validate request: %s", errors)
raise exceptions.InvalidJSONError(errors)
return func(self, *args, **kwargs)
return inner_decorator
return outer_decorator
评论列表
文章目录