Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation #705

Merged
merged 4 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
dist/
package-lock.json
3 changes: 3 additions & 0 deletions tests/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ beforeAll(() => {
setupMessages();
if (data == 3) {
outputInvalidJsonFile();

console.log("ERROR: JSON file is invalid. Either the file is not JSON, or there is an error in the JSON syntax.");
return process.exit(1)
};
Expand Down Expand Up @@ -52,8 +53,10 @@ test('Check if JSON file follows format', () => {

test('Check if JSON file matches the value name', () => {
const passed = getFileName(process.env.FILES, data);

let fileNameMessage = passed === true ? "File name matches target.RECORD_TYPE." : "File name does not match any target.RECORD_TYPE.";

core.setOutput('fileNameMessage', fileNameMessage);
expect(typeof passed).toBe('boolean'); // Check if the result is a boolean
expect(passed).toBe(true);
});
15 changes: 6 additions & 9 deletions utils/getFileName.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const { stripExt } = require('./utils.js');

async function getFileName(file, data) {
// Extract the file name without extension
function getFileName(file, data) {

const fileNameWithoutExt = stripExt(file);

// Validate if the file name matches any of the target.RECORD_TYPE values
const recordTypes = Object.keys(data.target);
if (!recordTypes.includes(fileNameWithoutExt.toUpperCase())) {
return false;
} else {
return true;
}
const recordTypes = Object.values(data.target);

return recordTypes[0].name == fileNameWithoutExt.toLowerCase();
}

module.exports = getFileName;
module.exports = getFileName;
6 changes: 5 additions & 1 deletion utils/getJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ const fs = require('fs');
const { getFileExtension, stripExt } = require('./utils.js');
const core = require('@actions/core');

function getJSON(file, filename) {
function getJSON(file) {

const path = `${process.env.actions_path}/${file}`; // File path.

const ext = getFileExtension(file);
if (path.includes("sub-logs") && !ext || ext != 'json') return 3 //if file is subdomain file but has no ext or diff ext than .json
if (!ext) return false; // If no file extension, return.
if (ext != 'json') return false; // If file extension is not '.json' return.

try {
if (fs.existsSync(path)) { // Check if file exists in domain directory

// It exists
const rawdata = fs.readFileSync(path); // Read the file
const data = JSON.parse(rawdata); // Parse it
Expand All @@ -19,6 +22,7 @@ function getJSON(file, filename) {
}
return 3; // It doesn't exist
} catch(err) {

core.setOutput('infoMessage', "Could not validate info.");
core.setOutput('recordMessage', "Could not validate records.");
core.setOutput('jsonData', "Could not read the JSON file, did you have an error in your syntax?")
Expand Down
Loading