Skip to content

Commit

Permalink
Merge pull request #1 from cmatthews444/Issue1217-csv-validation
Browse files Browse the repository at this point in the history
Boolean value validations
  • Loading branch information
SageMar authored Nov 28, 2024
2 parents 9722c00 + 67b8078 commit 62288b6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/server/services/csvPipeline/uploadMeters.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ async function uploadMeters(req, res, filepath, conn) {
try {
for (let i = 0; i < meters.length; i++) {
let meter = meters[i];
//validation for boolean values
validateBooleanFields(meter, i);


// First verify GPS is okay
// This assumes that the sixth column is the GPS as order is assumed for now in a GPS file.
const gpsInput = meter[6];
Expand Down Expand Up @@ -139,7 +143,8 @@ async function uploadMeters(req, res, filepath, conn) {
);
}
}
} catch (error) {
}
catch (error) {
throw new CSVPipelineError(`Failed to upload meters due to internal OED Error: ${error.message}`, undefined, 500);
}
}
Expand Down Expand Up @@ -240,4 +245,43 @@ async function getUnitId(unitName, expectedUnitType, conn) {
return unit.id;
}


/**
* Validates all boolean-like fields for a given meter row.
* @param {Array} meter - A single row from the CSV file.
* @param {number} rowIndex - The current row index for error reporting.
*/
function validateBooleanFields(meter, rowIndex) {
// all inputs that involve a true or false all bieng validated together.
const booleanFields = {
2: 'enabled',
3: 'displayable',
10: 'cumulative',
11: 'reset',
18: 'end only',
32: 'disableChecks'
};

for (const [index, name] of Object.entries(booleanFields)) {
let value = meter[index];

// allows upper/lower case.
if (typeof value === 'string') {
value = value.toLowerCase();
}

// Validates read values to either false or true
if (value !== 'true' && value !== 'false' && value !== true && value !== false) {
throw new CSVPipelineError(
`Invalid input for '${name}' in row ${rowIndex + 1}: "${meter[index]}". Expected 'true' or 'false'.`,
undefined,
500
);
}
}
}




module.exports = uploadMeters;

0 comments on commit 62288b6

Please sign in to comment.