- Input validation is tricky for this feature. - This is postponed until a future release.
46 lines
2.2 KiB
Python
46 lines
2.2 KiB
Python
from typing import List
|
|
from definitions.attributes import *
|
|
|
|
def validate_config(item_attributes: List[Attribute]) -> str:
|
|
"""
|
|
Validate the configuration file to ensure all attributes are properly defined.
|
|
Returns "Ok" if everything is valid, otherwise returns an error message.
|
|
"""
|
|
if not item_attributes:
|
|
return "Configuration must contain at least one attribute."
|
|
|
|
attrib_names = [attrib.attrib_name for attrib in item_attributes]
|
|
attrib_name_set = set(attrib_names)
|
|
|
|
# Check for duplicate attribute names
|
|
if len(attrib_names) != len(attrib_name_set):
|
|
return "Duplicate attribute names are not allowed."
|
|
|
|
# Map attribute names to their objects
|
|
attrib_map = {attrib.attrib_name: attrib for attrib in item_attributes}
|
|
|
|
# Validate each attribute
|
|
for attrib in item_attributes:
|
|
error = attrib.validate()
|
|
if error:
|
|
return error
|
|
|
|
# Validate comparison (if applicable)
|
|
if attrib.compareto:
|
|
return f"'{attrib.attrib_name}': 'compareto' feature is not fully implemented in this release."
|
|
''' WIP
|
|
if not isinstance(attrib.compareto, list) or not all(
|
|
isinstance(pair, tuple) and len(pair) == 2 for pair in attrib.compareto
|
|
):
|
|
return f"Invalid comparison format for attribute '{attrib.attrib_name}'. Expected a list of tuples."
|
|
for cmp, ref_attr_name in attrib.compareto:
|
|
if cmp not in VALID_COMPARISONS[attrib.html_input_type]:
|
|
return f"Invalid comparison '{cmp}' for attribute '{attrib.attrib_name}'. Allowed operators are: {VALID_COMPARISONS[attrib.html_input_type]}"
|
|
if ref_attr_name not in attrib_name_set:
|
|
return f"Invalid reference attribute '{ref_attr_name}' for comparison in attribute '{attrib.attrib_name}'."
|
|
ref_attr = attrib_map[ref_attr_name]
|
|
if ref_attr.html_input_type != attrib.html_input_type:
|
|
return f"Invalid comparison of '{attrib.attrib_name}' & '{ref_attr_name}' - must be of the same type, {attrib.html_input_type}."
|
|
'''
|
|
|
|
return "Ok" |