Introduced sets in config validation

This commit is contained in:
Candifloss 2025-03-04 12:27:20 +05:30
parent 783c25d0ad
commit bd0c1b2232
2 changed files with 22 additions and 12 deletions

View File

@ -8,17 +8,20 @@ db = SQLAlchemy()
# Mapping of attribute types to SQLAlchemy column types # Mapping of attribute types to SQLAlchemy column types
COLUMN_TYPE_MAPPING = { COLUMN_TYPE_MAPPING = {
textAttribute: lambda attrib: String(attrib.max_length), textAttribute: lambda attrib: String(attrib.max_length), # Map text attributes to String columns
dateAttribute: lambda attrib: Date, dateAttribute: lambda attrib: Date, # Map date attributes to Date columns
selectAttribute: lambda attrib: Enum(*attrib.options), selectAttribute: lambda attrib: Enum(*attrib.options), # Map select attributes to Enum columns
intAttribute: lambda attrib: Integer, intAttribute: lambda attrib: Integer, # Map integer attributes to Integer columns
floatAttribute: lambda attrib: Float, floatAttribute: lambda attrib: Float, # Map float attributes to Float columns
} }
def _get_column_type(attrib): def _get_column_type(attrib):
"""Helper function to get the SQLAlchemy column type for an attribute.""" """Helper function to get the SQLAlchemy column type for an attribute."""
if type(attrib) not in COLUMN_TYPE_MAPPING: if type(attrib) not in COLUMN_TYPE_MAPPING:
raise ValueError(f"Unsupported attribute type: {type(attrib)} for attribute '{attrib.attrib_name}'") raise ValueError(
f"Unsupported attribute type: {type(attrib)} for attribute '{attrib.attrib_name}'. "
f"Supported types are: {list(COLUMN_TYPE_MAPPING.keys())}"
)
return COLUMN_TYPE_MAPPING[type(attrib)](attrib) return COLUMN_TYPE_MAPPING[type(attrib)](attrib)
def _get_column_properties(attrib): def _get_column_properties(attrib):
@ -37,8 +40,8 @@ def create_asset_model():
# Define the table name and basic methods # Define the table name and basic methods
attrs = { attrs = {
'__tablename__': sql_conf.SQL_TABLE, '__tablename__': sql_conf.SQL_TABLE,
'__init__': lambda self, **kwargs: self.__dict__.update(kwargs), '__init__': lambda self, **kwargs: self.__dict__.update(kwargs), # Allow dynamic assignment of attributes
'__repr__': lambda self: f"<Asset {getattr(self, next(attrib.attrib_name for attrib in item_attributes if attrib.primary))}>" '__repr__': lambda self: f"<Asset {getattr(self, next(attrib.attrib_name for attrib in item_attributes if attrib.primary))}>" # Provide a readable representation of the object
} }
# Define columns based on item_attributes # Define columns based on item_attributes

View File

@ -1,14 +1,21 @@
from typing import List from typing import List
from definitions.attribute import Attribute from definitions.attribute import Attribute
VALID_OPERATORS = {"lt", "gt", "le", "ge", "eq"}
def validate_config(item_attributes: List[Attribute]) -> str: def validate_config(item_attributes: List[Attribute]) -> str:
""" """
Validate the configuration file to ensure all attributes are properly defined. Validate the configuration file to ensure all attributes are properly defined.
Returns "Ok" if everything is valid, otherwise returns an error message. Returns "Ok" if everything is valid, otherwise returns an error message.
""" """
# Check for duplicate attribute names if not item_attributes:
return "Configuration must contain at least one attribute."
attrib_names = [attrib.attrib_name for attrib in item_attributes] attrib_names = [attrib.attrib_name for attrib in item_attributes]
if len(attrib_names) != len(set(attrib_names)): 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." return "Duplicate attribute names are not allowed."
# Validate each attribute # Validate each attribute
@ -24,9 +31,9 @@ def validate_config(item_attributes: List[Attribute]) -> str:
): ):
return f"Invalid comparison format for attribute '{attrib.attrib_name}'. Expected a list of tuples." return f"Invalid comparison format for attribute '{attrib.attrib_name}'. Expected a list of tuples."
for cmp, ref_attr in attrib.compareto: for cmp, ref_attr in attrib.compareto:
if cmp not in ["lt", "gt", "le", "ge", "eq"]: if cmp not in VALID_OPERATORS:
return f"Invalid comparison operator '{cmp}' for attribute '{attrib.attrib_name}'." return f"Invalid comparison operator '{cmp}' for attribute '{attrib.attrib_name}'."
if ref_attr not in attrib_names: if ref_attr not in attrib_name_set:
return f"Invalid reference attribute '{ref_attr}' for comparison in attribute '{attrib.attrib_name}'." return f"Invalid reference attribute '{ref_attr}' for comparison in attribute '{attrib.attrib_name}'."
return "Ok" return "Ok"