Add column type mapping and enhance validation

Introduced a backend mapping for column expected types, improving validation and error handling. Updated UI to highlight default and corrected values, with additional detailed validation for data collection parameters.
This commit is contained in:
GotthardG
2025-01-07 16:07:13 +01:00
parent 92306fcfa6
commit 35369fd13c
4 changed files with 61 additions and 55 deletions

View File

@ -137,11 +137,12 @@ async def validate_cell(data: dict):
logger.info(f"Validating cell row {row_num}, column {col_name}, value {value}")
# Get the full data for the row
# Retrieve the full data for the row
current_row_data = row_storage.get_row(row_num)
if not current_row_data:
logger.error(f"No data found for row {row_num}")
# Explicitly return a 404 error if the row is missing
raise HTTPException(status_code=404, detail=f"No data found for row {row_num}")
try:
@ -152,33 +153,30 @@ async def validate_cell(data: dict):
cleaned_value = importer._clean_value(value, expected_type)
current_row_data[col_name] = cleaned_value # Update raw data
# If the column belongs to the nested `data_collection_parameters`
# Nested parameter handling for `DataCollectionParameters`
if col_name in DataCollectionParameters.model_fields:
# Ensure current_nested is a Pydantic model
nested_data = current_row_data.get("data_collection_parameters")
if isinstance(
nested_data, dict
): # If it's a dict, convert it to a Pydantic model
if isinstance(nested_data, dict):
# Convert dict to Pydantic model
current_nested = DataCollectionParameters(**nested_data)
elif isinstance(
nested_data, DataCollectionParameters
): # Already a valid model
elif isinstance(nested_data, DataCollectionParameters):
# Already a valid model
current_nested = nested_data
else: # If it's None or anything else, create a new instance
else:
current_nested = DataCollectionParameters()
# Convert the model to a dictionary, update the specific field, and
# re-create the Pydantic model
# Update the nested model's field and reapply validation
nested_params = current_nested.model_dump()
nested_params[col_name] = cleaned_value # Update the nested field
nested_params[col_name] = cleaned_value
current_row_data["data_collection_parameters"] = DataCollectionParameters(
**nested_params
)
return {"is_valid": True, "message": "", "corrected_value": cleaned_value}
except ValidationError as e:
# Handle and log errors
# Handle validation errors
logger.error(f"Validation error details: {e.errors()}")
column_error = next(
(err for err in e.errors() if err.get("loc")[0] == col_name), None
@ -188,7 +186,21 @@ async def validate_cell(data: dict):
f"Validation failed for row {row_num}, column {col_name}. Error: {message}"
)
return {"is_valid": False, "message": message}
except ValueError as e:
# Handle expected typecasting or value errors specifically
error_message = str(e)
logger.warning(
f"Failed to validate value '{value}' for row "
f"{row_num}, column {col_name}: {error_message}"
)
raise HTTPException(
status_code=400,
detail=f"Validation failed for row "
f"{row_num}, column {col_name}: {error_message}",
)
except Exception as e:
# Log unexpected issues
# Log unexpected issues and re-raise HTTP 500
logger.error(f"Unexpected error during validation: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error validating cell: {str(e)}")