Skip to content

Commit 420586c

Browse files
christian-byrnearjansingh
authored andcommitted
[ci] add JSON validation CI workflow (#5837)
## Summary Added GitHub Actions workflow and shell script to validate JSON syntax in repository files, as in the past we have committed locales files with invalid JSON. ## Changes - **What**: Added CI workflow for JSON validation with `jq` syntax checking - **Dependencies**: CI workflow requires `jq` (pre-installed on ubuntu-latest runners) ## Review Focus Script exclusion patterns for TSConfig files and environment variable override mechanism (`JSON_LINT_EXCLUDES`). ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-5837-ci-add-JSON-validation-CI-workflow-27c6d73d365081a199b1d9ae9e4edfa4) by [Unito](https://www.unito.io)
1 parent 1a2f915 commit 420586c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Validate JSON
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
json-lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Validate JSON syntax
15+
run: ./scripts/cicd/check-json.sh

scripts/cicd/check-json.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
usage() {
5+
echo "Usage: $0 [--debug]" >&2
6+
}
7+
8+
debug=0
9+
10+
while [ "$#" -gt 0 ]; do
11+
case "$1" in
12+
--debug)
13+
debug=1
14+
;;
15+
-h|--help)
16+
usage
17+
exit 0
18+
;;
19+
*)
20+
echo "Unknown option: $1" >&2
21+
usage
22+
exit 2
23+
;;
24+
esac
25+
shift
26+
done
27+
28+
# Validate JSON syntax in tracked files using jq
29+
if ! command -v jq >/dev/null 2>&1; then
30+
echo "Error: jq is required but not installed" >&2
31+
exit 127
32+
fi
33+
34+
EXCLUDE_PATTERNS=(
35+
'**/tsconfig*.json'
36+
)
37+
38+
if [ -n "${JSON_LINT_EXCLUDES:-}" ]; then
39+
# shellcheck disable=SC2206
40+
EXCLUDE_PATTERNS+=( ${JSON_LINT_EXCLUDES} )
41+
fi
42+
43+
pathspecs=(-- '*.json')
44+
for pattern in "${EXCLUDE_PATTERNS[@]}"; do
45+
if [[ ${pattern:0:1} == ':' ]]; then
46+
pathspecs+=("$pattern")
47+
else
48+
pathspecs+=(":(glob,exclude)${pattern}")
49+
fi
50+
done
51+
52+
mapfile -t json_files < <(git ls-files "${pathspecs[@]}")
53+
54+
if [ "${#json_files[@]}" -eq 0 ]; then
55+
echo 'No JSON files found.'
56+
exit 0
57+
fi
58+
59+
if [ "$debug" -eq 1 ]; then
60+
echo 'JSON files to validate:'
61+
printf ' %s\n' "${json_files[@]}"
62+
fi
63+
64+
failed=0
65+
for file in "${json_files[@]}"; do
66+
if ! jq -e . "$file" >/dev/null; then
67+
echo "Invalid JSON syntax: $file" >&2
68+
failed=1
69+
fi
70+
done
71+
72+
if [ "$failed" -ne 0 ]; then
73+
echo 'JSON validation failed.' >&2
74+
exit 1
75+
fi
76+
77+
echo 'All JSON files are valid.'

0 commit comments

Comments
 (0)