Skip to content
Latchkey

ajv-cli validate: JSON Schema Checks in CI

ajv validate -s schema.json -d data.json validates a data file against a JSON Schema and exits non-zero on any error.

ajv-cli wraps the Ajv validator so a pipeline can gate config on a schema without writing code. Point -s at the schema and -d at the data.

What it does

ajv-cli is the command-line front end for the Ajv JSON Schema validator. ajv validate loads a schema (-s) and one or more data files (-d), validates each, prints per-instance errors, and exits 1 if any file fails. It reads JSON and, with a plugin flag, YAML.

Common usage

Terminal
# install locally so CI pins the version
npm i -D ajv-cli ajv-formats

# validate one data file against a schema
npx ajv validate -s schema.json -d config.json

# validate many files by glob (quote the glob)
npx ajv validate -s schema.json -d "configs/*.json"

# enable format keywords (date, email, uri, etc.)
npx ajv validate -s schema.json -d config.json -c ajv-formats

Options

FlagWhat it does
-s <file>Schema file to validate against
-d <file|glob>Data file(s) to validate; quote globs
-r <file|glob>Referenced schemas to load ($ref targets)
-c <module>Load a plugin, e.g. -c ajv-formats
--spec=draft7|draft2019|draft2020Select the JSON Schema draft
--strict=falseDisable strict-mode errors on unknown keywords
--errors=text|json|lineError output format

In CI

A failing ajv validate returns exit code 1, so put it as a plain step and let the runner fail the job on a bad config. Pin the version with a devDependency (npx from a lockfile) so a new Ajv release does not change strict-mode behavior mid-pipeline.

Common errors in CI

A validation failure prints lines like config.json invalid followed by [ { instancePath: '/port', schemaPath: '#/properties/port/type', keyword: 'type', message: 'must be number' } ]. strict mode: unknown keyword: "X" means the schema uses a keyword Ajv does not know; add the plugin or pass --strict=false. unknown format "date-time" means you did not load ajv-formats (-c ajv-formats). no schema with key or ref "..." means a $ref target was not passed with -r.

Related guides

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