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:
@ -62,30 +62,22 @@ class SampleSpreadsheetImporter:
|
||||
return column_type_mapping.get(column_name, str)
|
||||
|
||||
def _clean_value(self, value, expected_type=None):
|
||||
"""Clean value by converting it to the expected type and handle edge cases."""
|
||||
if value is None:
|
||||
return None
|
||||
if expected_type == str:
|
||||
# Ensure value is converted to string and stripped of whitespace
|
||||
return str(value).strip()
|
||||
if expected_type in [float, int]:
|
||||
try:
|
||||
return expected_type(value)
|
||||
except (ValueError, TypeError):
|
||||
# If conversion fails, return None
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
# Handle numeric strings
|
||||
if "." in value:
|
||||
return float(value)
|
||||
else:
|
||||
return int(value)
|
||||
except ValueError:
|
||||
pass
|
||||
# In case of failure, return the stripped string
|
||||
return value.strip()
|
||||
# If no expected type or value type match, return the original value
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.error(
|
||||
f"Failed to cast value '{value}' to {expected_type}. Error: {e}"
|
||||
)
|
||||
raise ValueError(
|
||||
f"Invalid value: '{value}'. Expected type: {expected_type}."
|
||||
)
|
||||
# Fallback for unhandled types
|
||||
logger.warning(f"Unhandled type for value: '{value}'. Returning as-is.")
|
||||
return value
|
||||
|
||||
def import_spreadsheet(self, file):
|
||||
|
Reference in New Issue
Block a user