2025-02-12 20:25:37 +00:00
|
|
|
import re
|
|
|
|
from datetime import datetime
|
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():
|
|
|
|
# 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}'."
|
2025-02-12 20:25:37 +00:00
|
|
|
elif attrib.html_input_type =="number": # Validate numbers
|
|
|
|
if attrib.min:
|
|
|
|
try:
|
|
|
|
min_val = int(attrib.min)
|
|
|
|
except:
|
|
|
|
return f"Min value for {attrib_name} must be a valid number."
|
|
|
|
if attrib.max:
|
|
|
|
try:
|
|
|
|
max_val = int(attrib.max)
|
|
|
|
except:
|
|
|
|
return f"Max value for {attrib_name} must be a valid number."
|
|
|
|
if attrib.min and attrib.max and max_val < min_val:
|
|
|
|
return f"Max value must greater than min value for attribute '{attrib_name}'"
|
|
|
|
if attrib.default_val:
|
|
|
|
try:
|
|
|
|
def_val = int(attrib.default_val)
|
|
|
|
except:
|
|
|
|
return f"Default value for {attrib_name} must be a valid number."
|
|
|
|
if attrib.min and def_val < attrib.min:
|
|
|
|
return f"Default value for {attrib_name} must be a greater or equal to min value."
|
|
|
|
if attrib.max and def_val > attrib.max:
|
|
|
|
return f"Default value for {attrib_name} must be a less or equal to max value."
|
|
|
|
elif attrib.html_input_type =="date": # Validate dates
|
|
|
|
if attrib.min:
|
|
|
|
try:
|
|
|
|
min_val = datetime.strptime(attrib.min, "%Y-%m-%d")
|
|
|
|
except:
|
|
|
|
return f"Min value for {attrib_name} must be a valid date in YYYY-MM-DD format."
|
|
|
|
if attrib.max:
|
|
|
|
try:
|
|
|
|
max_val = datetime.strptime(attrib.max, "%Y-%m-%d")
|
|
|
|
except:
|
|
|
|
return f"Max value for {attrib_name} must be a valid date in YYYY-MM-DD format."
|
|
|
|
if attrib.min and attrib.max and max_val < min_val:
|
|
|
|
return f"Max value must greater than min value for attribute '{attrib_name}'"
|
|
|
|
if attrib.default_val:
|
|
|
|
try:
|
|
|
|
def_val = datetime.strptime(attrib.default_val, "%Y-%m-%d")
|
|
|
|
except:
|
|
|
|
return f"Default value for {attrib_name} must be a valid date in YYYY-MM-DD format."
|
|
|
|
if attrib.min and def_val < attrib.min:
|
|
|
|
return f"Default value for {attrib_name} must be a greater or equal to min value."
|
|
|
|
if attrib.max and def_val > attrib.max:
|
|
|
|
return f"Default value for {attrib_name} must be a less or equal to max value."
|
|
|
|
elif attrib.html_input_type =="text": # Validate text types
|
|
|
|
if attrib.min or attrib.max or attrib.auto_increment or attrib.options:
|
|
|
|
return f"Text attribute {attrib_name} can't have min, max, auto_increment, or options."
|
|
|
|
if attrib.default_val:
|
|
|
|
try:
|
|
|
|
def_val = str(attrib.default_val)
|
|
|
|
if attrib.regex and not re.match(attrib.regex, def_val):
|
|
|
|
return f"Default value for {attrib_name} is invalid. Allowed pattern: {attrib.regex}."
|
|
|
|
except:
|
|
|
|
return f"Unexpected default value for text attribute '{attrib_name}.'"
|
|
|
|
elif attrib.html_input_type == "select": # Validate selection menu types
|
|
|
|
if not attrib.options:
|
|
|
|
return f"Selection missing options for attribute '{attrib_name}'."
|
|
|
|
if attrib.default_val and attrib.default_val not in attrib.options:
|
|
|
|
return f"Default value for '{attrib_name}' must be in the options."
|
|
|
|
|
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 10:29:36 +00:00
|
|
|
|
|
|
|
return "Ok"
|