Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from chalice import Response
- from cerberus import Validator
- def validate(schema, location="body"):
- def decorator(fn):
- def wrapped_f(*args, **kwargs):
- v = Validator(schema, allow_unknown=True)
- data = {}
- if location == "body":
- data = app.current_request.json_body or data
- if location == "query":
- data = app.current_request.query_params or data
- if v(data):
- return fn(*args, **kwargs)
- body = {"message": f"Invalid {location} parameters.", "data": v.errors}
- return Response(body=body, status_code=400)
- """
- # Sample usage:
- todo_list_params = {
- "keyword": {"type": "string", "required": False, "empty": False},
- "start_date": {"type": "datetime", "required": False, "empty": False,
- "coerce": lambda date: datetime.datetime.strptime(date, "%Y-%m-%d")},
- }
- @app.route("/todos", methods=["GET"])
- @validate(todo_list_params, location="query")
- def get_todo_list():
- return {"todos": []}
- """
Add Comment
Please, Sign In to add comment