2025-02-22 18:58:10 +00:00
|
|
|
from typing import List
|
|
|
|
from definitions.attribute import Attribute
|
2025-02-12 10:29:36 +00:00
|
|
|
|
2025-02-22 18:58:10 +00:00
|
|
|
def validate_config(item_attributes: List[Attribute]) -> str:
|
2025-02-19 10:38:10 +00:00
|
|
|
"""
|
|
|
|
Validate the configuration file to ensure all attributes are properly defined.
|
|
|
|
Returns "Ok" if everything is valid, otherwise returns an error message.
|
|
|
|
"""
|
2025-02-22 18:58:10 +00:00
|
|
|
# Check for duplicate attribute names
|
|
|
|
attrib_names = [attrib.attrib_name for attrib in item_attributes]
|
|
|
|
if len(attrib_names) != len(set(attrib_names)):
|
|
|
|
return "Duplicate attribute names are not allowed."
|
2025-02-12 20:54:33 +00:00
|
|
|
|
2025-02-22 18:58:10 +00:00
|
|
|
# Validate each attribute
|
|
|
|
for attrib in item_attributes:
|
|
|
|
error = attrib.validate()
|
|
|
|
if error:
|
|
|
|
return error
|
2025-02-12 20:54:33 +00:00
|
|
|
|
2025-02-19 10:38:10 +00:00
|
|
|
# Validate comparison (if applicable)
|
2025-02-12 10:29:36 +00:00
|
|
|
if attrib.compareto:
|
2025-02-22 18:58:10 +00:00
|
|
|
if not isinstance(attrib.compareto, list) or not all(
|
|
|
|
isinstance(pair, tuple) and len(pair) == 2 for pair in attrib.compareto
|
|
|
|
):
|
2025-02-19 10:38:10 +00:00
|
|
|
return f"Invalid comparison format for attribute '{attrib.attrib_name}'. Expected a list of tuples."
|
|
|
|
for cmp, ref_attr in attrib.compareto:
|
|
|
|
if cmp not in ["lt", "gt", "le", "ge", "eq"]:
|
|
|
|
return f"Invalid comparison operator '{cmp}' for attribute '{attrib.attrib_name}'."
|
2025-02-22 18:58:10 +00:00
|
|
|
if ref_attr not in attrib_names:
|
2025-02-19 10:38:10 +00:00
|
|
|
return f"Invalid reference attribute '{ref_attr}' for comparison in attribute '{attrib.attrib_name}'."
|
|
|
|
|
2025-02-12 10:29:36 +00:00
|
|
|
return "Ok"
|