Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Apr 20, 2024
2 parents 6926fd2 + c9d8856 commit 47fc786
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 22 deletions.
69 changes: 69 additions & 0 deletions .github/scripts/validate-slash_commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const fs = require('fs');
const path = require('path');

const directory = process.argv[2]; // Take directory as an input argument

let foundErrors = false;

try {
const files = fs.readdirSync(directory);

if (files.length === 0) {
console.log("No JSON files found in the directory.");
process.exit(0);
}

console.log(`Found ${files.length} files in the directory.`);

files.forEach(file => {
if (!file.endsWith('.json')) {
console.log(`Skipping non-JSON file: ${file}`);
return;
}

const filePath = path.join(directory, file);
try {
const data = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(data);

console.log(`Processing ${file}...`);

function checkFields(obj, path = '') {
Object.keys(obj).forEach(key => {
const value = obj[key];
const currentPath = path ? `${path}.${key}` : key;

if (typeof value === 'string') {
if (path.endsWith('.description') && value.length > 100) {
console.error(`${file}: Description exceeds 100 characters at '${currentPath}'`);
foundErrors = true;
}
if (path.endsWith('.name') && value.includes(' ')) {
console.error(`${file}: Name contains space at '${currentPath}'`);
foundErrors = true;
}
} else if (typeof value === 'object' && value !== null) {
checkFields(value, currentPath);
}
});
}

checkFields(jsonData);

} catch (err) {
console.error(`Error processing ${file}:`, err);
foundErrors = true;
}
});
} catch (err) {
console.error("Error reading the directory:", err);
process.exit(1);
}

if (foundErrors) {
console.error("Validation errors found in the files. Please check the logs above.");
process.exit(1);
} else {
console.log("All files validated successfully.");
process.exit(0);
}
33 changes: 33 additions & 0 deletions .github/scripts/validate-slash_commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
directory=$1

valid=true

for file in "$directory"/*.json; do
echo "Validating $file"

# Validate 'name' fields: No spaces allowed
name_issues=$(jq -r 'recurse | objects | .name? | select(.) | with_entries(select(.value | test(" "))) | keys[] as $keys | $keys[] | "\($keys) at line \(input_line_number)"' "$file")
if [ ! -z "$name_issues" ]; then
echo "Validation failed in $file for 'name' field(s) with spaces:"
echo "$name_issues"
valid=false
fi

description_issues=$(jq -r 'recurse | objects | .description? | select(.) | to_entries[] | select(.value | type == "string" and length > 100) | "\(.key) at line \(input_line_number)"' "$file")
if [ ! -z "$description_issues" ]; then
echo "Validation failed in $file for 'description' field(s) exceeding 100 characters:"
echo "$description_issues"
valid=false
fi

if [ -z "$name_issues" ] && [ -z "$description_issues" ]; then
echo "$file validated successfully."
fi
done

if [ "$valid" = false ]; then
echo "Validation errors found in some JSON files."
exit 1
else
echo "All JSON files validated successfully."
fi
2 changes: 1 addition & 1 deletion .github/workflows/validate-slash_commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
- uses: actions/checkout@v2

- name: Validate JSON files
run: bash ./scripts/validate-slash_commands.sh ./slash_commands
run: node .github/scripts/validate-slash_commands.js ./slash_commands
21 changes: 0 additions & 21 deletions scripts/validate-slash_commands.sh

This file was deleted.

0 comments on commit 47fc786

Please sign in to comment.