diff options
Diffstat (limited to 'uploader/input_validation.py')
-rw-r--r-- | uploader/input_validation.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/uploader/input_validation.py b/uploader/input_validation.py index 88ffd8c..b84fca6 100644 --- a/uploader/input_validation.py +++ b/uploader/input_validation.py @@ -1,6 +1,11 @@ """Input validation utilities""" +import re +import json +import base64 from typing import Any +from flask import request + def is_empty_string(value: str) -> bool: """Check whether as string is empty""" return (isinstance(value, str) and value.strip() == "") @@ -50,3 +55,19 @@ def is_valid_representative_name(repr_name: str) -> bool: """ pattern = re.compile(r"^[a-zA-Z]+[a-zA-Z0-9_-]*[a-zA-Z0-9]$") return bool(pattern.match(repr_name)) + + +def encode_errors(errors: tuple[tuple[str, str], ...], form) -> str: + """Encode form errors into base64 string.""" + return base64.b64encode( + json.dumps({ + "errors": dict(errors), + "original_formdata": dict(form) + }).encode("utf8")) + + +def decode_errors(errorstr) -> dict[str, dict]: + """Decode errors from base64 string""" + if not bool(errorstr): + return {"errors": {}, "original_formdata": {}} + return json.loads(base64.b64decode(errorstr.encode("utf8")).decode("utf8")) |