From d3c038c4b14e20e430530f7a565a4c0af171a56b Mon Sep 17 00:00:00 2001 From: jcpitre Date: Tue, 30 Jan 2024 19:26:27 -0500 Subject: [PATCH] Changed to make the code hopefully a bit more understandable. --- gbfs-validator/gbfs.js | 77 ++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/gbfs-validator/gbfs.js b/gbfs-validator/gbfs.js index 8c43f91..c538cad 100644 --- a/gbfs-validator/gbfs.js +++ b/gbfs-validator/gbfs.js @@ -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. @@ -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 { @@ -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