import { MobileCardConfig } from '../components/MobileView/types' export interface ValidationResult { valid: boolean errors: string[] warnings: string[] } export function validateMobileCardConfig( config: MobileCardConfig ): ValidationResult { const errors: string[] = [] const warnings: string[] = [] if (!config.titleKey && !config.titleRender) { errors.push('Mobile card config must have either titleKey or titleRender') } if (!config.primaryFields || config.primaryFields.length === 0) { warnings.push('Mobile card config has no primary fields defined') } if (config.primaryFields) { config.primaryFields.forEach((field, index) => { if (!field.key && !field.render) { errors.push(`Primary field at index ${index} must have either key or render function`) } }) } if (config.secondaryFields) { config.secondaryFields.forEach((field, index) => { if (!field.key && !field.render) { errors.push(`Secondary field at index ${index} must have either key or render function`) } }) } if (config.actions) { config.actions.forEach((action, index) => { if (!action.id) { errors.push(`Action at index ${index} must have an id`) } if (!action.onClick) { errors.push(`Action '${action.id || index}' must have an onClick handler`) } if (!action.label && !action.icon) { warnings.push(`Action '${action.id || index}' should have either a label or icon`) } }) } if (config.showSelection && !config.onSelectionChange) { warnings.push('showSelection is enabled but onSelectionChange handler is not provided') } return { valid: errors.length === 0, errors, warnings, } } export function validateColumnConfig(columns: any[]): ValidationResult { const errors: string[] = [] const warnings: string[] = [] if (!columns || columns.length === 0) { errors.push('At least one column must be defined') return { valid: false, errors, warnings } } const columnIds = new Set() columns.forEach((column, index) => { if (!column.id && !column.accessorKey) { errors.push(`Column at index ${index} must have either id or accessorKey`) } const columnId = column.id || column.accessorKey if (columnIds.has(columnId)) { errors.push(`Duplicate column id: '${columnId}'`) } columnIds.add(columnId) if (!column.header && !column.hideHeader) { warnings.push(`Column '${columnId}' has no header defined`) } if (!column.cell && !column.accessorKey && !column.accessorFn) { warnings.push(`Column '${columnId}' has no way to access data (missing cell, accessorKey, or accessorFn)`) } }) return { valid: errors.length === 0, errors, warnings, } } export function logValidationResults( configName: string, result: ValidationResult, throwOnError: boolean = false ): void { if (result.errors.length > 0) { console.error(`[${configName}] Validation Errors:`, result.errors) if (throwOnError) { throw new Error(`${configName} validation failed: ${result.errors.join(', ')}`) } } if (result.warnings.length > 0) { console.warn(`[${configName}] Validation Warnings:`, result.warnings) } if (result.valid && result.warnings.length === 0) { console.log(`[${configName}] Configuration is valid`) } }