# ============================================================ # utils/validators.py # ============================================================ import pandas as pd def validate_files_loaded(source_df, target_df): """ Checks whether both source and target files are loaded. """ if source_df is None or target_df is None: return False, "Please upload both Source and Target files." if len(source_df) == 0: return False, "Source file has no rows." if len(target_df) == 0: return False, "Target file has no rows." return True, "Files are valid." def validate_common_columns(source_df, target_df): """ Checks whether source and target have at least one common column. """ source_cols = set(source_df.columns) target_cols = set(target_df.columns) common_cols = list(source_cols.intersection(target_cols)) if not common_cols: return False, "No common columns found between Source and Target." return True, common_cols def validate_key_columns(source_df, target_df, key_columns): """ Validates selected key columns. """ if not key_columns: return False, "Please select at least one key column." missing_source = [ col for col in key_columns if col not in source_df.columns ] missing_target = [ col for col in key_columns if col not in target_df.columns ] if missing_source: return False, f"Key column(s) missing in Source: {missing_source}" if missing_target: return False, f"Key column(s) missing in Target: {missing_target}" source_nulls = source_df[key_columns].isnull().any(axis=1).sum() target_nulls = target_df[key_columns].isnull().any(axis=1).sum() if source_nulls > 0 or target_nulls > 0: return False, ( f"Null key values found. " f"Source null-key rows: {source_nulls}, " f"Target null-key rows: {target_nulls}" ) return True, "Key columns are valid." def find_duplicate_keys(df, key_columns): """ Returns duplicate key rows. """ if not key_columns: return pd.DataFrame() return df[ df.duplicated( subset=key_columns, keep=False ) ].copy() def validate_scope_range(start, end, total_rows): """ Validates row range selection. """ if start < 1: return False, "Start row cannot be less than 1." if end > total_rows: return False, "End row cannot be greater than total rows." if start > end: return False, "Start row cannot be greater than end row." return True, "Scope range is valid."