Merge pull request #705 from DEV-DIBSTER/main

fix: validation
This commit is contained in:
Tweak
2024-09-20 20:04:42 -06:00
committed by GitHub
4 changed files with 17 additions and 10 deletions
+3
View File
@@ -0,0 +1,3 @@
node_modules/
dist/
package-lock.json
+3
View File
@@ -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)
};
@@ -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);
});
+6 -9
View File
@@ -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;
+5 -1
View File
@@ -2,8 +2,10 @@ 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.
@@ -11,6 +13,7 @@ function getJSON(file, filename) {
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
@@ -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?")