def schema(path=None):
"""Validate the request payload with a JSONSchema.
Decorator func that will be used to specify
the path to the schema that the route/endpoint
will be validated against.
:param path: path to the schema file
:type path: string
:returns: list of errors if there are any
:raises: InvalidAPIRequest if there are any errors
ex:
@schema('/path/to/schema.json')
@app.route('/app-route')
def app_route():
...
"""
def decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
_path = path.lstrip('/')
schema_path = os.path.join(SCHEMAS_PATH, request.blueprint, _path)
payload = get_request_payload(request.method)(request)
errors = validate_schema(payload, get_schema(schema_path))
if errors:
raise InvalidAPIRequest(message=errors)
return func(*args, **kwargs)
return wrapped
return decorator
评论列表
文章目录