Skip to content

6.0 Enhancement

6.0 Enhancement #4

Workflow file for this run

name: Validate JSON Files
on:
push:
paths:
- '**/*.json'
pull_request:
paths:
- '**/*.json'
workflow_dispatch: # Allows manual triggering
jobs:
validate-json:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install dependencies
run: npm install -g ajv-cli ajv-formats
- name: Create JSON schema for validation
run: |
cat > schema.json << 'EOL'
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"required": ["version", "new"],
"properties": {
"version": {
"type": "string",
"pattern": "^\\d+\\.\\d+$|^\\d+\\.\\d+\\.\\d+$"
},
"subVersion": {
"type": "string",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"new": {
"type": "array",
"items": {
"type": "object",
"required": ["icon", "title", "subtitle", "body"],
"properties": {
"icon": { "type": "string" },
"title": { "type": "string" },
"subtitle": { "type": "string" },
"body": { "type": "string" }
}
},
"minItems": 1
}
}
},
"minItems": 1
}
EOL
- name: Scan for JSON files
id: scan
run: |
echo "Scanning for JSON files..."
# Create a temporary file to store the list of JSON files
touch json_files.txt
# Find all data.json files and group them by language
find Demo -name "data.json" -print0 | while IFS= read -r -d '' FILE; do
# Extract the language from the directory path
LANG_DIR=$(dirname "$FILE")
LANG=$(basename "$LANG_DIR")
echo "$LANG:$FILE" >> json_files.txt
done
echo "Found the following JSON files for validation:"
cat json_files.txt
echo "Scan completed."
- name: Validate JSON syntax per language
run: |
echo "Validating JSON syntax for each language..."
ERROR=0
# Process each file by language
while IFS=':' read -r LANG_DIR FILE || [ -n "$FILE" ]; do
echo "Checking syntax for language: $LANG_DIR - $FILE"
# Check JSON syntax
if ! jq empty "$FILE" 2>/dev/null; then
echo "❌ Invalid JSON syntax in $LANG_DIR ($FILE)"
ERROR=1
else
echo "✅ JSON syntax is valid for $LANG_DIR"
fi
done < json_files.txt
if [ $ERROR -ne 0 ]; then
echo "JSON syntax validation failed"
exit 1
fi
- name: Validate schema per language
run: |
echo "Validating JSON schema for each language..."
ERROR=0
# Process each file by language
while IFS=':' read -r LANG_DIR FILE || [ -n "$FILE" ]; do
echo "Validating schema for language: $LANG_DIR - $FILE"
# Validate against schema
if ! npx ajv -s schema.json -d "$FILE" --strict=false; then
echo "❌ $LANG_DIR ($FILE) does not match the required schema"
ERROR=1
else
echo "✅ Schema is valid for $LANG_DIR"
fi
done < json_files.txt
if [ $ERROR -ne 0 ]; then
echo "JSON schema validation failed"
exit 1
fi
echo "All JSON files are valid! 🎉"