How to Validate JSON Against Schemas in GitHub Actions
A typo in a JSON config can break runtime config with no compiler to catch it; schema validation makes it a CI failure.
Run ajv validate against each config file and its schema so any deviation fails the job.
Steps
- Define a JSON Schema for each config file you want to guard.
- Install
ajv-cliand runajv validate -s schema.json -d config.json. - Let a validation failure exit non-zero and fail the build.
Workflow
.github/workflows/validate-json.yml
name: Validate JSON
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm i -g ajv-cli ajv-formats
- run: ajv validate -c ajv-formats -s schemas/config.schema.json -d 'config/*.json'Notes
- Add
ajv-formatsif your schema uses formats likedate-timeoremail. - On Latchkey managed runners validation jobs run cheaper and self-heal if a runner drops.
Related guides
How to Validate OpenAPI Specs in GitHub ActionsLint and validate an OpenAPI spec on every pull request in GitHub Actions with Redocly or Spectral so a broke…
How to Lint YAML with yamllint in GitHub ActionsLint YAML files in GitHub Actions with yamllint so indentation and syntax mistakes fail CI before they break…