Skip to content

Commit

Permalink
Changed to make the code hopefully a bit more understandable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpitre committed Jan 31, 2024
1 parent 46cdef4 commit d3c038c
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions gbfs-validator/gbfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,59 @@ const validatorVersion = process.env.COMMIT_REF
* }} VehicleType
*/

/**
* This function returns true if the file from a GBFS feed has errors or if the file is required and missing.
* @param {Object} data - The body of a file from a GBFS feed.
* @param {boolean} required - True if the file is required.
* @returns {boolean}
/**
* Look into the array of files data and return true if any has an error.
* @param {FileValidationResult[]} files A list of files data to check for errors.
* @returns {boolean} True if any of the files has an error.
*/
function hasErrors(data, required) {
let hasError = false
function filesHaveErrors(files) {
if (Array.isArray(files)) {
return files.some((file) => hasErrors(file, file.required));
}
// If the argument is not an array of file data, we'll say there is no error
return false;
}

for (let i = 0; i < data.length; i++) {
const el = data[i];
/**
* Return true if the file data from a GBFS feed has errors or if the file is required and missing.
* The file data can be an array (e.g. for multi-language) or directly the body from the file.
* @param {Object} data - The body of a file data from a GBFS feed.
* @returns {boolean}
*/
function fileHasErrors(fileData, required) {
if (fileHasMultiLanguages(fileData)) {
return fileData.some((languageBody) => hasErrors(languageBody, required))
}
// So it's not a multi-language array, just check the data directly.
return hasErrors(fileData, required)
}

if (Array.isArray(el)) {
if (hasErrors(el, required)) {
return true
}
} else {
if (typeof required === 'undefined') {
// If the required boolean is not specified, use the required of each individual file
if (el.required && !el.exists) {
return true
}
} else if (required && !el.exists) {
return true
}
// At this point we know there are no errors because a required file does not exists. Now check for json errors
if (!!el.errors || el.hasErrors) {
return true
}
}
/**
* This function returns true if the file data from a GBFS feed has errors or if the file is required and missing.
* Note that a file data cannot be an array for multi-languages.
* @param {Object} data - The body of a file data from a GBFS feed.
* @returns {boolean}
*/
function hasErrors(fileData, required) {
if (required && !fileData.exists) {
return true
}
if (!!fileData.errors || fileData.hasErrors) {
return true
}
return false
}

/**
* This function returns true if the file data from a GBFS feed is an multi-language array of file data.
* @param {Object} data - The body of a file data from a GBFS feed.
* @returns {boolean}
*/
function fileHasMultiLanguages(fileData) {
// Currently the only way we know it's multi-language is if it's an array
return Array.isArray(fileData)
}

/**
* This function returns the number of errors in a file of a GBFS feed.
* @param {FileValidationResult} file - The validation result of a file from a GBFS feed.
Expand Down Expand Up @@ -532,7 +551,7 @@ class GBFS {
? body.reduce((acc, l) => acc && l.exists, true)
: false,
file: `${type}.json`,
hasErrors: hasErrors(body, required)
hasErrors: fileHasErrors(body, required)
}
} else {
return {
Expand Down Expand Up @@ -801,7 +820,7 @@ class GBFS {
detected: result[0].version,
validated: this.options.version || result[0].version
},
hasErrors: hasErrors(result),
hasErrors: filesHaveErrors(result),
errorsCount: filesResult.reduce((acc, file) => {
acc += file.errorsCount
return acc
Expand Down

0 comments on commit d3c038c

Please sign in to comment.