aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/authorisation/resources/request_utils.py
blob: 03d3c3be21cba2f44245856c1ef6ab8b778b5ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""Some common utils for requests to the resources endpoints."""
from functools import reduce

from pymonad.either import Left, Right, Either

def check_form(form, *fields) -> Either:
    """Check form for errors"""
    def __check_field__(errors, field):
        if not bool(form.get(field)):
            return errors + (f"Missing `{field}` value.")
        return errors

    errors = reduce(__check_field__, fields, tuple())
    if len(errors) > 0:
        return Left({
            "error": "Invalid request data!",
            "error_description": "\n\t - ".join(errors)
        })

    return Right(form)