Skip to content

Added tests

Added tests #297

Workflow file for this run

name: JSON Checker
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
jobs:
validate-json:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v4
# Step 2: Set up Node.js
- name: Set up Node.js
uses: actions/setup-node@v4.1.0
with:
node-version: 20
# Step 3: Install jsonlint
- name: Install jsonlint
run: npm install -g jsonlint
# Step 4: Find and validate JSON files
- name: Validate JSON files
run: |
set -e
# Function to validate JSON files in a directory or a single file
validate_json() {
local path="$1"
local max_size=$((5 * 1024 * 1024)) # 5MB limit
# Define the validate_file function inside validate_json
validate_file() {
local file="$1"
echo "Validating $file"
file_size=$(wc -c < "$file" | tr -d " ")
# Check if file size is available and numeric
if [ -z "$file_size" ] || ! [ "$file_size" -eq "$file_size" ] 2>/dev/null; then
echo -e "\033[33mWarning: Unable to determine file size for $file\033[0m"
else
# Ensure file size does not exceed max limit
if [ "$file_size" -gt "$max_size" ]; then
echo -e "\033[31mError: $file exceeds 5MB limit\033[0m"
exit 1
fi
fi
# Validate JSON and exit with an error if it fails
jsonlint --quiet "$file" || exit 1
}
if [ -e "$path" ]; then
echo "Checking $path"
if [ -d "$path" ]; then
# If path is a directory, find all JSON files and validate them
find "$path" -type f -not -type l -name "*.json" -print0 | while IFS= read -r -d '' file; do
validate_file "$file"
done
else
# Validate a single file
if [ -L "$path" ]; then
echo -e "\033[31mError: $path is a symlink\033[0m"
exit 1
fi
validate_file "$path"
fi
else
echo -e "\033[31mWarning: $path does not exist\033[0m"
fi
}
# Validate JSON files in specified locations
validate_json "config"
echo -e "\033[32mAll files in folder config/ validated.\033[0m"
validate_json "deployments"
echo -e "\033[32mAll files in folder deployments/ validated.\033[0m"
validate_json "audit/auditLog.json"
echo -e "\033[32mauditLog.json validated.\033[0m"
validate_json "script/deploy/_targetState.json"
echo -e "\033[32m_targetState.json validated.\033[0m"
validate_json "script/deploy/resources/deployRequirements.json"
echo -e "\033[32mdeployRequirements.json validated.\033[0m"