Team,
I need a javascript function to validate csv file is conform to a schema definition.
BU_NAME,BU_CONTACT,CONTEXT
US_BU,John,FIN
US_BU,Emily,SCM
,Eric,SCM
UK_BU,Joe,SCM
The above input file should throw error in line 3 as the mandatory BU_NAME is blank. But the below JS code didn’t fall in catch block. It always returns 200 but I need 500 when there is csv format issue encountered.
resolve(fileSet) {
const validator = require('xsd-schema-validator');
let status = '';
if (fileSet.length > 0) {
//Grab the first (and only) file
let csvFile = fileSet[0];
//Check it's the correct type
try {
if (csvFile.type === 'text/csv') {
alert('CSV FILE');
//Create a File reader and its onload callback
let fileReader = new FileReader();
fileReader.onload = function (fileReadEvent) {
let readCSVData = fileReadEvent.target.result;
alert('result 11===> ' + readCSVData);
// resolve(readCSVData);
};
fileReader.readAsText(csvFile);
status = 200;
}
} catch (error) {
alert('status ===> ' + status+ ' error ==> '+ error);
return status = 500;
}
}
return status;
}