diff --git a/definitions/attribute.py b/definitions/attribute.py index 6e34cef..c58eaf6 100644 --- a/definitions/attribute.py +++ b/definitions/attribute.py @@ -1,6 +1,6 @@ from datetime import datetime import re -from typing import List, Optional, Tuple +from typing import List, Optional, Tuple, Set from dataclasses import dataclass, field ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"} @@ -16,6 +16,7 @@ class Attribute: primary: bool = False default_val: Optional[str] = None compareto: Optional[List[Tuple[str, str]]] = None + valid_comparisons: Optional[Set[str]] = None def validate(self) -> Optional[str]: """Validate common attributes. Returns an error message if invalid, otherwise None.""" @@ -40,6 +41,7 @@ class textAttribute(Attribute): def __post_init__(self): """Post-initialization to set the HTML input type.""" self.html_input_type = "text" + self.valid_comparisons = {"lt", "st", "eq", "ne"}, # longer than, shorter than, eq, or not eq, to the ref attrib def validate(self) -> Optional[str]: """Validate text-specific attributes.""" @@ -78,6 +80,7 @@ class intAttribute(Attribute): def __post_init__(self): """Post-initialization to set the HTML input type.""" self.html_input_type = "number" + self.valid_comparisons = {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != def validate(self) -> Optional[str]: """Validate integer-specific attributes.""" @@ -118,6 +121,7 @@ class floatAttribute(Attribute): def __post_init__(self): """Post-initialization to set the HTML input type.""" self.html_input_type = "number" + self.valid_comparisons = {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != def validate(self) -> Optional[str]: """Validate float-specific attributes.""" @@ -157,6 +161,7 @@ class dateAttribute(Attribute): def __post_init__(self): """Post-initialization to set the HTML input type.""" self.html_input_type = "date" + self.valid_comparisons = {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != def _is_date(self, value: str) -> bool: """Check if a value is a valid date in YYYY-MM-DD format.""" @@ -202,6 +207,7 @@ class selectAttribute(Attribute): def __post_init__(self): """Post-initialization to set the HTML input type.""" self.html_input_type = "select" + self.valid_comparisons = {"eq", "ne"} # eq, or not eq, to the ref attrib def validate(self) -> Optional[str]: """Validate select-specific attributes.""" diff --git a/functions/validate_config.py b/functions/validate_config.py index 09e7fc6..d6d1191 100644 --- a/functions/validate_config.py +++ b/functions/validate_config.py @@ -1,15 +1,6 @@ from typing import List from definitions.attribute import Attribute -#VALID_OPERATORS = {"lt", "gt", "le", "ge", "eq", "ne"} # <, >, <=, >=, ==, != - -VALID_COMPARISONS = { - "number": {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != - "text": {"lt", "st", "eq", "ne"}, # longer than, shorter than, eq, or not eq, to the ref attrib - "date": {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != - "select": {"eq", "ne"} # eq, or not eq, to the ref attrib -} - def validate_config(item_attributes: List[Attribute]) -> str: """ Validate the configuration file to ensure all attributes are properly defined. @@ -38,8 +29,8 @@ def validate_config(item_attributes: List[Attribute]) -> str: ): 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 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 cmp not in attrib.valid_comparisons: + return f"Invalid comparison '{cmp}' for attribute '{attrib.attrib_name}'. Allowed operators are: {attrib.valid_comparisons}" if ref_attr not in attrib_name_set: return f"Invalid reference attribute '{ref_attr}' for comparison in attribute '{attrib.attrib_name}'." if ref_attr.html_input_type != attrib.html_input_type: