flask_crud_app/functions/validate_config.py

34 lines
1.7 KiB
Python
Raw Normal View History

2025-02-12 10:29:36 +00:00
from functions.validate_values import validate_value
# Validate the configuration file to ensure all attributes are properly defined.
def validate_config(item_attributes):
for attrib_name, attrib in item_attributes.items():
# Check for required fields
if not attrib.display_name:
return f"Missing display name for attribute '{attrib_name}'."
if not attrib.html_input_type:
return f"Missing input type for attribute '{attrib_name}'."
elif attrib.html_input_type not in ["text", "number", "date", "select"]:
return f"Invalid input type for attribute '{attrib_name}'."
elif attrib.html_input_type == "select" and not attrib.options:
return f"Selection missing options for attribute '{attrib_name}'."
# Validate min and max values
if attrib.min or attrib.max:
if attrib.html_input_type not in ["number", "date"]:
return f"{attrib.display_name} must be of type 'number' or 'date' to have min or max values."
# Validate default value
if attrib.default_val:
validation_result = validate_value(attrib, attrib.default_val)
if validation_result != "Ok":
return f"Invalid default value for attribute '{attrib_name}': {validation_result}"
# Validate comparison
if attrib.compareto:
if attrib.compareto[0] not in ["lt", "gt", "le", "ge", "eq"]:
return f"Invalid comparison operator for attribute '{attrib_name}'. Valid operators are: lt, gt, le, ge, eq."
if attrib.compareto[1] not in item_attributes:
return f"Invalid reference attribute '{attrib.compareto[1]}' for comparison in attribute '{attrib_name}'."
return "Ok"