Skip to content

Commit

Permalink
Fix JSON test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jul 28, 2024
1 parent a7242c5 commit 25e4fd6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tests/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const ajv = new Ajv({ strict: false, allErrors: true });
addFormats(ajv);
require("ajv-errors")(ajv);

const schemaFile = await fs.readFile("tests/schemas/entries.json", "utf8");
const schema = JSON.parse(schemaFile);
const validate = ajv.compile(schema);
let errors = false;

/**
Expand All @@ -26,18 +23,21 @@ function error(message, properties) {

async function main() {
const files = process.argv.slice(2);
const schemaFile = await fs.readFile("tests/schemas/entries.json", "utf8");
const schema = JSON.parse(schemaFile);
const validate = ajv.compile(schema);

await Promise.all(
files.map(async (file) => {
try {
const json = await JSON.parse(await fs.readFile(file, "utf8"));
const entry = json[Object.keys(json)[0]];
validateJSONSchema(file, json);
validateJSONSchema(file, json, validate);
validateFileContents(file, entry);
} catch (e) {
error(`Failed to process ${file}: ${e.message}`, { file });
}
}),
})
);

process.exit(+errors);
Expand All @@ -48,8 +48,9 @@ async function main() {
*
* @param {string} file - File path to be validated.
* @param {object} json - Parsed JSON content of the file.
* @param validate - AJV object
*/
function validateJSONSchema(file, json) {
function validateJSONSchema(file, json, validate) {
const valid = validate(json);
if (!valid) {
errors = true;
Expand Down Expand Up @@ -78,7 +79,7 @@ function validateFileContents(file, entry) {
if (entry.url === `https://${entry.domain}`)
error(`Unnecessary url element defined.`, { file });

if (entry['img'] === `${entry.domain}.svg`)
if (entry["img"] === `${entry.domain}.svg`)
error(`Unnecessary img element defined.`, { file });

if (file !== `entries/${entry.domain[0]}/${valid_name}`)
Expand All @@ -93,7 +94,7 @@ function validateFileContents(file, entry) {
if (entry.tfa && !entry.documentation)
core.warning(
"No documentation set. Please provide screenshots in the pull request",
{ file, title: "Missing documentation" },
{ file, title: "Missing documentation" }
);
}

Expand Down

0 comments on commit 25e4fd6

Please sign in to comment.