32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import typing
|
|
from marshmallow import ValidationError
|
|
|
|
class InputValidationBase:
|
|
"""
|
|
Base class for input validators. Common validation methods are grouped here.
|
|
"""
|
|
|
|
@staticmethod
|
|
def check_required_fields(content:dict, required_fields:list[str]):
|
|
missing_fields = [field for field in required_fields if content.get(field) is None]
|
|
if missing_fields:
|
|
raise ValidationError({"required_fields": f"Missing required fields: {missing_fields}"})
|
|
|
|
@staticmethod
|
|
def check_if_entry_is_already_deleted(entry:dict):
|
|
"""
|
|
Checks if the entry has 'deleted' set to True/1.
|
|
"""
|
|
if entry.get("deleted") in [True, 1, "1"]:
|
|
raise ValidationError({"deleted": "The selected entry is already deleted."})
|
|
|
|
@staticmethod
|
|
def check_deleted_flag_on_post(content:dict):
|
|
"""
|
|
Checks if 'deleted' is set to 1/True in a POST (Create) request.
|
|
"""
|
|
if content.get("deleted") in [True, 1, "1"]:
|
|
raise ValidationError({"deleted": "Cannot create an entry with 'deleted' set to 1."})
|
|
|
|
|