From 8e807fa52c201c36fddd10d635287ee9ed35dae5 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sat, 15 Mar 2025 09:42:47 +0530 Subject: [PATCH] VALID_COMPARISONS --- definitions/attributes/Attribute.py | 8 +++++++- definitions/attributes/__init__.py | 3 ++- definitions/attributes/dateAttribute.py | 1 - definitions/attributes/numAttribute.py | 1 - definitions/attributes/selectAttribute.py | 1 - definitions/attributes/textAttribute.py | 1 - functions/validate_config.py | 16 ++++++++++------ 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/definitions/attributes/Attribute.py b/definitions/attributes/Attribute.py index aeca201..1acc787 100644 --- a/definitions/attributes/Attribute.py +++ b/definitions/attributes/Attribute.py @@ -2,6 +2,12 @@ from typing import List, Optional, Tuple, Set from dataclasses import dataclass, field ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"} +VALID_COMPARISONS = { + "text": {"lt", "st", "eq", "ne"}, # longer than, shorter than, eq, or not eq, to the ref attrib + "number": {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != + "date": {"lt", "gt", "le", "ge", "eq", "ne"}, # <, >, <=, >=, ==, != + "select": {"eq", "ne"} # eq, or not eq, to the ref attrib +} @dataclass class Attribute: @@ -14,7 +20,7 @@ class Attribute: primary: bool = False default_val: Optional[str] = None compareto: Optional[List[Tuple[str, str]]] = None - valid_comparisons: Optional[Set[str]] = None + #valid_comparisons: Optional[Set[str]] = None def validate(self) -> Optional[str]: """Validate common attributes. Returns an error message if invalid, otherwise None.""" diff --git a/definitions/attributes/__init__.py b/definitions/attributes/__init__.py index b57ecee..9ef4e2c 100644 --- a/definitions/attributes/__init__.py +++ b/definitions/attributes/__init__.py @@ -1,4 +1,4 @@ -from .Attribute import Attribute +from .Attribute import Attribute, VALID_COMPARISONS from .textAttribute import textAttribute from .numAttribute import numAttribute, intAttribute, floatAttribute from .dateAttribute import dateAttribute @@ -13,4 +13,5 @@ __all__ = [ "floatAttribute", "dateAttribute", "selectAttribute", + "VALID_COMPARISONS", ] \ No newline at end of file diff --git a/definitions/attributes/dateAttribute.py b/definitions/attributes/dateAttribute.py index b47d267..0529094 100644 --- a/definitions/attributes/dateAttribute.py +++ b/definitions/attributes/dateAttribute.py @@ -12,7 +12,6 @@ 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.""" diff --git a/definitions/attributes/numAttribute.py b/definitions/attributes/numAttribute.py index 43deaa1..4952f9e 100644 --- a/definitions/attributes/numAttribute.py +++ b/definitions/attributes/numAttribute.py @@ -12,7 +12,6 @@ class numAttribute(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 numeric-specific attributes.""" diff --git a/definitions/attributes/selectAttribute.py b/definitions/attributes/selectAttribute.py index 03fd0bb..83da365 100644 --- a/definitions/attributes/selectAttribute.py +++ b/definitions/attributes/selectAttribute.py @@ -10,7 +10,6 @@ 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/definitions/attributes/textAttribute.py b/definitions/attributes/textAttribute.py index 790f600..e4a0382 100644 --- a/definitions/attributes/textAttribute.py +++ b/definitions/attributes/textAttribute.py @@ -14,7 +14,6 @@ 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.""" diff --git a/functions/validate_config.py b/functions/validate_config.py index 1e46eec..a126247 100644 --- a/functions/validate_config.py +++ b/functions/validate_config.py @@ -16,6 +16,9 @@ def validate_config(item_attributes: List[Attribute]) -> str: 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() @@ -28,12 +31,13 @@ def validate_config(item_attributes: List[Attribute]) -> str: 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 in attrib.compareto: - 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}'." + 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}' & '{ref_attr}' - must be of the same 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" \ No newline at end of file