-
-
Notifications
You must be signed in to change notification settings - Fork 12
243 lines (202 loc) · 8.53 KB
/
validate-json.yml
File metadata and controls
243 lines (202 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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! 🎉"
check-localization-completeness:
runs-on: ubuntu-latest
needs: validate-json
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Find JSON files
id: find-json
run: |
# Create a directory to store extracted language files
mkdir -p extracted_langs
# Find the base (en) language JSON file
BASE_FILE=$(find Demo -path "*/en.lproj/data.json" -print -quit)
if [ ! -f "$BASE_FILE" ]; then
echo "❌ Base language file (en.lproj/data.json) not found!"
exit 1
fi
# Find all localized JSON files
find Demo -name "data.json" | grep -v "$BASE_FILE" > localized_files.txt
echo "Base file: $BASE_FILE"
echo "Found $(wc -l < localized_files.txt) localized files"
- name: Check for missing versions
run: |
BASE_FILE=$(find Demo -path "*/en.lproj/data.json" -print -quit)
# Extract version numbers from base file
jq -r '.[].version' "$BASE_FILE" | sort -V > base_versions.txt
# Create a report file
touch localization_report.md
echo "# Localization Completeness Report" >> localization_report.md
echo "Generated on $(date)" >> localization_report.md
echo "" >> localization_report.md
# Check each localized file
while read -r LOC_FILE; do
LANG_DIR=$(dirname "$LOC_FILE")
LANG=$(basename "$LANG_DIR" | sed 's/\.lproj//')
echo "## Checking $LANG" >> localization_report.md
# Extract version numbers from localized file
jq -r '.[].version' "$LOC_FILE" | sort -V > "${LANG}_versions.txt"
# Find missing versions
MISSING_VERSIONS=$(comm -23 base_versions.txt "${LANG}_versions.txt")
if [ -n "$MISSING_VERSIONS" ]; then
echo "❌ $LANG is missing versions: $MISSING_VERSIONS"
echo "### Missing versions:" >> localization_report.md
echo '```' >> localization_report.md
echo "$MISSING_VERSIONS" >> localization_report.md
echo '```' >> localization_report.md
echo "" >> localization_report.md
EXIT_CODE=1
else
echo "✅ $LANG has all versions from base language"
echo "✅ All versions present" >> localization_report.md
echo "" >> localization_report.md
fi
# Check content for each version
echo "### Content comparison:" >> localization_report.md
while read -r VERSION; do
# Extract base data for this version
jq -r --arg v "$VERSION" '.[] | select(.version == $v)' "$BASE_FILE" > "base_${VERSION}.json"
jq -r --arg v "$VERSION" '.[] | select(.version == $v)' "$LOC_FILE" > "${LANG}_${VERSION}.json"
if [ -s "${LANG}_${VERSION}.json" ]; then
# Count items in the "new" array for base and localized
BASE_ITEMS=$(jq -r '.new | length' "base_${VERSION}.json")
LOC_ITEMS=$(jq -r '.new | length' "${LANG}_${VERSION}.json")
if [ "$BASE_ITEMS" -ne "$LOC_ITEMS" ]; then
echo "❌ $LANG version $VERSION has $LOC_ITEMS items (base has $BASE_ITEMS)"
echo "- Version $VERSION: $LOC_ITEMS/$BASE_ITEMS items ❌" >> localization_report.md
EXIT_CODE=1
else
echo "✅ $LANG version $VERSION has all $BASE_ITEMS items"
echo "- Version $VERSION: $LOC_ITEMS/$BASE_ITEMS items ✅" >> localization_report.md
fi
fi
done < base_versions.txt
echo "" >> localization_report.md
done < localized_files.txt
cat localization_report.md
# Exit with the accumulated status
if [ "$EXIT_CODE" -eq 1 ]; then
echo "❌ Localization check failed - some languages are missing versions or items"
exit 1
else
echo "✅ All languages have complete translations"
fi
- name: Upload Localization Report
if: always()
uses: actions/upload-artifact@v4
with:
name: localization-report
path: localization_report.md