Skip to content
Latchkey

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-cli and run ajv 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-formats if your schema uses formats like date-time or email.
  • On Latchkey managed runners validation jobs run cheaper and self-heal if a runner drops.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →