Skip to content

Commit

Permalink
fix: added helper function for getting the error index
Browse files Browse the repository at this point in the history
  • Loading branch information
MacQSL committed Jan 8, 2025
1 parent c16c240 commit 3b675d8
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions api/src/utils/csv-utils/csv-config-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ export const executeRowValidator = (params: CSVRowParams, rowValidator: CSVRowVa
values: error.values ?? null,
cell: error.cell ?? null,
header: error.header ?? null,
// WorksheetRowIndexSymbol is the original row index from the worksheet ie: before filtering empty rows
row: error.row ?? params.row[WorksheetRowIndexSymbol] + 1 ?? params.rowIndex + 2 // headers: 1, data row: 2
row: _getErrorRowIndex(params, error.row)
});
});
};
Expand Down Expand Up @@ -311,8 +310,7 @@ export const executeValidateCell = (
values: error.values ?? null,
cell: (error.cell === undefined ? params.cell : error.cell) ?? null, // Use cell value if intentionally null
header: (error.header === undefined ? params.header : error.header) ?? null, // Use header value if intentionally null
// WorksheetRowIndexSymbol is the original row index from the worksheet ie: before filtering empty rows
row: error.row ?? params.row[WorksheetRowIndexSymbol] + 1 ?? params.rowIndex + 2 // headers: 1, data row: 2
row: _getErrorRowIndex(params, error.row)
});
});
}
Expand Down Expand Up @@ -343,3 +341,28 @@ export const _getCSVStaticHeaderMap = (config: CSVConfig) => {

return headerMap;
};

/**
* Get the row index the error occurred on.
*
* Note: Header row index 1. First data row index 2.
* Note: `WorksheetRowIndexSymbol` is the original row index from the worksheet ie: before filtering empty rows
*
* @param {CSVRowParams} params - The CSV row or cell parameters
* @param {number} [errorIndex] - The error index
* @returns {*} {number} - The error row index
*/
const _getErrorRowIndex = (params: { row: CSVRow; rowIndex: number }, errorIndex?: number) => {
// If the error index is provided use that
if (errorIndex) {
return errorIndex;

Check warning on line 358 in api/src/utils/csv-utils/csv-config-validation.ts

View check run for this annotation

Codecov / codecov/patch

api/src/utils/csv-utils/csv-config-validation.ts#L358

Added line #L358 was not covered by tests
}

// This is injected by the `getWorksheetRowObjects` function
if (params.row[WorksheetRowIndexSymbol]) {
return params.row[WorksheetRowIndexSymbol] + 1;
}

// Params row index is 0 based
return params.rowIndex + 2;

Check warning on line 367 in api/src/utils/csv-utils/csv-config-validation.ts

View check run for this annotation

Codecov / codecov/patch

api/src/utils/csv-utils/csv-config-validation.ts#L367

Added line #L367 was not covered by tests
};

0 comments on commit 3b675d8

Please sign in to comment.