Skip to content

Commit 33fab0a

Browse files
committed
Add object name validation
1 parent 1e81a01 commit 33fab0a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

index.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const inFile = core.getInput('in-file');
77
const inDirectory = core.getInput('in-directory');
88
const outFile = core.getInput('out-file');
99

10+
const nameRegex = /^[a-zA-Z]([_a-zA-Z0-9]?[a-zA-Z0-9])*$/
11+
1012
try
1113
{
1214
core.info("inFile: " + inFile);
@@ -22,6 +24,19 @@ try
2224
{
2325
const fileData = fs.readFileSync(inFile, 'utf8');
2426
outputJson = JSON.parse(fileData);
27+
28+
// Validate object names
29+
let anyFailed = false;
30+
Object.keys(outputJson).forEach(key =>
31+
{
32+
if(!nameRegex.test(key))
33+
{
34+
core.error(`Object name ${key} from ${inFile} is not a valid object name`);
35+
anyFailed = true;
36+
}
37+
});
38+
if(anyFailed)
39+
return;
2540
}
2641
else
2742
core.error("inDirectory " + inDirectory + " does not exist");
@@ -36,14 +51,24 @@ try
3651
if (!filename.endsWith(".json"))
3752
return;
3853

39-
const filenameWithoutExtension = filename.substring(0, filename.length - ".json".length);
40-
if (!outputJson.hasOwnProperty(filenameWithoutExtension)) {
41-
core.error(filenameWithoutExtension + " is already defined (in " + inFile + ")");
54+
const objectName = filename.substring(0, filename.length - ".json".length);
55+
56+
// Validate name usability
57+
if(!nameRegex.test(objectName))
58+
{
59+
core.error(objectName + " is invalid name for an object");
60+
return;
61+
}
62+
63+
// Check for duplicates
64+
if (!outputJson.hasOwnProperty(objectName))
65+
{
66+
core.error(objectName + " is already defined (in " + inFile + ")");
4267
return;
4368
}
4469

4570
const fileData = fs.readFileSync(inDirectory + '/' + filename, {encoding: 'utf8', flag: 'r'});
46-
outputJson[filenameWithoutExtension] = JSON.parse(fileData);
71+
outputJson[objectName] = JSON.parse(fileData);
4772
});
4873
}
4974
else
@@ -62,7 +87,6 @@ try
6287
{ encoding: 'utf8', flag: 'w' }
6388
);
6489
}
65-
6690
}
6791
catch(error)
6892
{

0 commit comments

Comments
 (0)