2025-02-12 20:25:37 +00:00
|
|
|
import re
|
|
|
|
from datetime import datetime
|
2025-02-12 10:29:36 +00:00
|
|
|
|
2025-02-12 20:54:33 +00:00
|
|
|
def _is_int(value):
|
|
|
|
"""Check if a value is a valid integer."""
|
|
|
|
try:
|
|
|
|
int(value)
|
|
|
|
return True
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _is_date(value):
|
|
|
|
"""Check if a value is a valid date in YYYY-MM-DD format."""
|
|
|
|
try:
|
|
|
|
datetime.strptime(value, "%Y-%m-%d")
|
|
|
|
return True
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _validate_number(attrib, attrib_name):
|
|
|
|
"""Validate number-specific attributes."""
|
|
|
|
if attrib.min and not _is_int(attrib.min):
|
|
|
|
return False
|
|
|
|
if attrib.max and not _is_int(attrib.max):
|
|
|
|
return False
|
|
|
|
if attrib.min and attrib.max and int(attrib.max) < int(attrib.min):
|
|
|
|
return False
|
|
|
|
if attrib.default_val and not _is_int(attrib.default_val):
|
|
|
|
return False
|
|
|
|
if attrib.default_val:
|
|
|
|
if attrib.min and int(attrib.default_val) < int(attrib.min):
|
|
|
|
return False
|
|
|
|
if attrib.max and int(attrib.default_val) > int(attrib.max):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _validate_date(attrib, attrib_name):
|
|
|
|
"""Validate date-specific attributes."""
|
|
|
|
if attrib.min and not _is_date(attrib.min):
|
|
|
|
return False
|
|
|
|
if attrib.max and not _is_date(attrib.max):
|
|
|
|
return False
|
|
|
|
if attrib.min and attrib.max and datetime.strptime(attrib.max, "%Y-%m-%d") < datetime.strptime(attrib.min, "%Y-%m-%d"):
|
|
|
|
return False
|
|
|
|
if attrib.default_val and not _is_date(attrib.default_val):
|
|
|
|
return False
|
|
|
|
if attrib.default_val:
|
|
|
|
if attrib.min and datetime.strptime(attrib.default_val, "%Y-%m-%d") < datetime.strptime(attrib.min, "%Y-%m-%d"):
|
|
|
|
return False
|
|
|
|
if attrib.max and datetime.strptime(attrib.default_val, "%Y-%m-%d") > datetime.strptime(attrib.max, "%Y-%m-%d"):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _validate_text(attrib, attrib_name):
|
|
|
|
"""Validate text-specific attributes."""
|
|
|
|
if attrib.min or attrib.max or attrib.auto_increment or attrib.options:
|
|
|
|
return False
|
|
|
|
if attrib.default_val and attrib.regex and not re.match(attrib.regex, str(attrib.default_val)):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _validate_select(attrib, attrib_name):
|
|
|
|
"""Validate select-specific attributes."""
|
|
|
|
if not attrib.options:
|
|
|
|
return False
|
|
|
|
if attrib.default_val and attrib.default_val not in attrib.options:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2025-02-12 10:29:36 +00:00
|
|
|
# Validate the configuration file to ensure all attributes are properly defined.
|
|
|
|
def validate_config(item_attributes):
|
|
|
|
for attrib_name, attrib in item_attributes.items():
|
2025-02-12 20:54:33 +00:00
|
|
|
# Validate display_name and html_input_type
|
2025-02-12 10:29:36 +00:00
|
|
|
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}'."
|
2025-02-12 20:54:33 +00:00
|
|
|
if attrib.html_input_type not in ["text", "number", "date", "select"]:
|
2025-02-12 10:29:36 +00:00
|
|
|
return f"Invalid input type for attribute '{attrib_name}'."
|
2025-02-12 20:54:33 +00:00
|
|
|
|
|
|
|
# Validate min, max, and default values based on input type
|
|
|
|
if attrib.html_input_type == "number":
|
|
|
|
if not _validate_number(attrib, attrib_name):
|
|
|
|
return f"Invalid number configuration for attribute '{attrib_name}'."
|
|
|
|
elif attrib.html_input_type == "date":
|
|
|
|
if not _validate_date(attrib, attrib_name):
|
|
|
|
return f"Invalid date configuration for attribute '{attrib_name}'."
|
|
|
|
elif attrib.html_input_type == "text":
|
|
|
|
if not _validate_text(attrib, attrib_name):
|
|
|
|
return f"Invalid text configuration for attribute '{attrib_name}'."
|
|
|
|
elif attrib.html_input_type == "select":
|
|
|
|
if not _validate_select(attrib, attrib_name):
|
|
|
|
return f"Invalid select configuration for attribute '{attrib_name}'."
|
|
|
|
|
2025-02-12 10:29:36 +00:00
|
|
|
# Validate min and max values
|
2025-02-12 20:25:37 +00:00
|
|
|
if (attrib.min or attrib.max) and attrib.html_input_type not in ['number', 'date']:
|
|
|
|
return f"'{attrib_name}' must be of type 'number' or 'date' to have min or max values."
|
2025-02-12 10:29:36 +00:00
|
|
|
|
|
|
|
# Validate comparison
|
|
|
|
if attrib.compareto:
|
|
|
|
if attrib.compareto[1] not in item_attributes:
|
|
|
|
return f"Invalid reference attribute '{attrib.compareto[1]}' for comparison in attribute '{attrib_name}'."
|
2025-02-12 20:25:37 +00:00
|
|
|
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."
|
2025-02-12 20:54:33 +00:00
|
|
|
# Return "Ok" if everything is valid, otherwise return an error message.
|
2025-02-12 10:29:36 +00:00
|
|
|
return "Ok"
|