From a4ff6cbaab75597ad2fc3b9990935f69c13adbc9 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sun, 20 Feb 2022 14:45:00 +0100 Subject: [PATCH] added quote length validation --- frontend/json-validation.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/frontend/json-validation.js b/frontend/json-validation.js index 4a26f0059..0bf917e5f 100644 --- a/frontend/json-validation.js +++ b/frontend/json-validation.js @@ -344,6 +344,8 @@ function validateQuotes() { let quoteFilesErrors; let quoteIdsAllGood = true; let quoteIdsErrors; + let quoteLengthsAllGood = true; + let quoteLengthErrors = []; const quotesFiles = fs.readdirSync("./static/quotes/"); quotesFiles.forEach((quotefilename) => { quotefilename = quotefilename.split(".")[0]; @@ -375,6 +377,22 @@ function validateQuotes() { quoteIdsAllGood = false; quoteIdsErrors = quoteIdsValidator.errors; } + const incorrectQuoteLength = quoteData.quotes + .filter((quote) => quote.text.length !== quote.length) + .map((quote) => quote.id); + if (incorrectQuoteLength.length !== 0) { + console.log( + `Quote ${quotefilename} ID(s) ${incorrectQuoteLength.join( + "," + )} length fields are \u001b[31mincorrect\u001b[0m` + ); + quoteFilesAllGood = false; + incorrectQuoteLength.map((id) => { + quoteLengthErrors.push( + `${quotefilename} ${id} length field is incorrect` + ); + }); + } }); if (quoteFilesAllGood) { console.log(`Quote file JSON schemas are \u001b[32mvalid\u001b[0m`); @@ -388,6 +406,12 @@ function validateQuotes() { console.log(`Quote IDs are \u001b[31mnot unique\u001b[0m`); return reject(new Error(quoteIdsErrors)); } + if (quoteLengthsAllGood) { + console.log(`Quote length fields are \u001b[32mcorrect\u001b[0m`); + } else { + console.log(`Quote length fields are \u001b[31mincorrect\u001b[0m`); + return reject(new Error(quoteLengthErrors)); + } resolve(); }); }