Introduced sets in config validation
This commit is contained in:
parent
783c25d0ad
commit
bd0c1b2232
@ -8,17 +8,20 @@ db = SQLAlchemy()
|
||||
|
||||
# Mapping of attribute types to SQLAlchemy column types
|
||||
COLUMN_TYPE_MAPPING = {
|
||||
textAttribute: lambda attrib: String(attrib.max_length),
|
||||
dateAttribute: lambda attrib: Date,
|
||||
selectAttribute: lambda attrib: Enum(*attrib.options),
|
||||
intAttribute: lambda attrib: Integer,
|
||||
floatAttribute: lambda attrib: Float,
|
||||
textAttribute: lambda attrib: String(attrib.max_length), # Map text attributes to String columns
|
||||
dateAttribute: lambda attrib: Date, # Map date attributes to Date columns
|
||||
selectAttribute: lambda attrib: Enum(*attrib.options), # Map select attributes to Enum columns
|
||||
intAttribute: lambda attrib: Integer, # Map integer attributes to Integer columns
|
||||
floatAttribute: lambda attrib: Float, # Map float attributes to Float columns
|
||||
}
|
||||
|
||||
def _get_column_type(attrib):
|
||||
"""Helper function to get the SQLAlchemy column type for an attribute."""
|
||||
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)
|
||||
|
||||
def _get_column_properties(attrib):
|
||||
@ -37,8 +40,8 @@ def create_asset_model():
|
||||
# Define the table name and basic methods
|
||||
attrs = {
|
||||
'__tablename__': sql_conf.SQL_TABLE,
|
||||
'__init__': lambda self, **kwargs: self.__dict__.update(kwargs),
|
||||
'__repr__': lambda self: f"<Asset {getattr(self, next(attrib.attrib_name for attrib in item_attributes if attrib.primary))}>"
|
||||
'__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))}>" # Provide a readable representation of the object
|
||||
}
|
||||
|
||||
# Define columns based on item_attributes
|
||||
|
@ -1,14 +1,21 @@
|
||||
from typing import List
|
||||
from definitions.attribute import Attribute
|
||||
|
||||
VALID_OPERATORS = {"lt", "gt", "le", "ge", "eq"}
|
||||
|
||||
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.
|
||||
"""
|
||||
# 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]
|
||||
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."
|
||||
|
||||
# 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."
|
||||
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}'."
|
||||
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 "Ok"
|
Loading…
Reference in New Issue
Block a user