Example #1
0
def post_validation(req: BaseRequest, *args) -> Tuple[dict, Optional[Error]]:
    if req.method != "POST":
        return {}, Error(405, "GET method is not allowed.")
    if req.get_header("Content-Type") != "application/json":
        return {}, Error(400, "HTTP request header `Content-Type` must be `application/json`.")
    if req.body.getvalue().decode() == "":
        return {}, Error(400, "Request body is empty.")
    if not isinstance(req.json, dict):
        return {}, Error(400, "Request body is not JSON string.")
    for k in args:
        if not req.json.get(k):
            return {}, Error(400, f"The field {k} does not exist in the request body.")
    return req.json, None