diff --git a/public/assets/js/csvxmlV2.js b/public/assets/js/csvxmlV2.js index ab5a0a0..43943c3 100644 --- a/public/assets/js/csvxmlV2.js +++ b/public/assets/js/csvxmlV2.js @@ -44,6 +44,7 @@ class CsvxmlValidator { validate() { this.validateMandatoryTagsPresent(); + this.validateInvalidTagsPresent(); this.checkDuplicateInvNos(); this.checkDependentColumns(); this.checkControlledLists(); @@ -73,6 +74,18 @@ class CsvxmlValidator { } + validateInvalidTagsPresent() { + + const headers = Object.keys(this.toValidate[0]); + + for (let header of headers) { + if (this.fieldList[header] === undefined) { + this.errors.parsing.push("Invalid column " + header + " detected! Please remove this column or use the appropriate name!"); + } + } + + } + checkDuplicateInvNos() { let invNoEncountered = []; @@ -107,6 +120,7 @@ class CsvxmlValidator { for (let fieldName in line) { if (line[fieldName] === '') continue; + if (this.fieldList[fieldName] === undefined) continue; // This may be the case if invalid fields are present const dependencies = this.fieldList[fieldName].dependsOn; if (dependencies === undefined) continue; diff --git a/public/assets/js/csvxmlV2.min.js b/public/assets/js/csvxmlV2.min.js index c4eb86d..c5b9160 100644 --- a/public/assets/js/csvxmlV2.min.js +++ b/public/assets/js/csvxmlV2.min.js @@ -2,14 +2,15 @@ class CsvxmlValidator{fieldList;toValidate;errors;constructor(fieldList,csvRaw){this.errors={parsing:[],mandatoryTags:[],duplicateInvNos:[],dependentColumns:[],controlledLists:[],mainImageResource:[],};this.fieldList=Object.freeze(fieldList);const data=Papa.parse(csvRaw.trim(),{header:true});if(data.errors.length!==0){window.alert(data.errors);} let toValidate=data.data;this.toValidate=toValidate;if(toValidate.length===0){alert("Error: No lines of content identified");} this.validate();} -validate(){this.validateMandatoryTagsPresent();this.checkDuplicateInvNos();this.checkDependentColumns();this.checkControlledLists();} +validate(){this.validateMandatoryTagsPresent();this.validateInvalidTagsPresent();this.checkDuplicateInvNos();this.checkDependentColumns();this.checkControlledLists();} validateMandatoryTagsPresent(){let mandatoryFields=[];for(let fieldName in this.fieldList){if(this.fieldList[fieldName].required===true){mandatoryFields.push(fieldName);}} console.log(this.toValidate);let lineCounter=1;for(let line of this.toValidate){for(let mandatoryField of mandatoryFields){if(line[mandatoryField]===undefined||line[mandatoryField]===null||line[mandatoryField]===''){this.errors.mandatoryTags.push("Missing or empty mandatory tag "+mandatoryField+" on line "+lineCounter);}} lineCounter++;}} +validateInvalidTagsPresent(){const headers=Object.keys(this.toValidate[0]);for(let header of headers){if(this.fieldList[header]===undefined){this.errors.parsing.push("Invalid column "+header+" detected! Please remove this column or use the appropriate name!");}}} checkDuplicateInvNos(){let invNoEncountered=[];let lineCounter=1;for(let line of this.toValidate){if(invNoEncountered.includes(line.inventory_number)){this.errors.duplicateInvNos.push("Duplicate inventory number "+line.inventory_number+" on line "+lineCounter);} invNoEncountered.push(line.inventory_number);lineCounter++;}} checkDependentColumns(){const headers=Object.keys(this.toValidate[0]);for(let header of headers){if(this.fieldList[header]===undefined||this.fieldList[header].dependsOn===undefined||this.fieldList[header].dependsOn===null)continue;let dependencies=this.fieldList[header].dependsOn;for(let dep of dependencies){if(headers.includes(dep)===false){this.errors.dependentColumns.push("Dependency issue at column "+header+": Corresponding column "+dep+" is missing");}}} -let lineCounter=1;for(let line of this.toValidate){for(let fieldName in line){if(line[fieldName]==='')continue;const dependencies=this.fieldList[fieldName].dependsOn;if(dependencies===undefined)continue;for(let dependency of dependencies){if(line[dependency]===''){this.errors.dependentColumns.push("Dependency issue at column "+fieldName+": Corresponding column "+dependency+" is empty");}}} +let lineCounter=1;for(let line of this.toValidate){for(let fieldName in line){if(line[fieldName]==='')continue;if(this.fieldList[fieldName]===undefined)continue;const dependencies=this.fieldList[fieldName].dependsOn;if(dependencies===undefined)continue;for(let dependency of dependencies){if(line[dependency]===''){this.errors.dependentColumns.push("Dependency issue at column "+fieldName+": Corresponding column "+dependency+" is empty");}}} lineCounter++;}} checkControlledLists(){let lineCounter=1;for(let line of this.toValidate){for(let fieldName in line){if(this.fieldList[fieldName]===undefined){console.log("Undefined but requested field "+fieldName);continue;} const allowedValues=this.fieldList[fieldName].allowedValues;if(allowedValues===undefined||allowedValues===null)continue;if(Object.values(allowedValues).length===0||Object.values(allowedValues).includes(line[fieldName])){continue;} diff --git a/public/index.htm b/public/index.htm index 8d0e9b9..e5066bf 100644 --- a/public/index.htm +++ b/public/index.htm @@ -4,7 +4,7 @@ - + @@ -27,7 +27,7 @@ - + \ No newline at end of file diff --git a/scripts/compile.php b/scripts/compile.php index aa15033..9ad61e9 100644 --- a/scripts/compile.php +++ b/scripts/compile.php @@ -7,7 +7,7 @@ declare(strict_types = 1); require_once __DIR__ . "/../functions/functions.php"; -const GET_PARAM_JS_CSS = "v000006"; +const GET_PARAM_JS_CSS = "v000008"; /** * Generates the json for a translation file.