Skip to content
Latchkey

ajv validate: Validate JSON Against a Schema

ajv validate -s schema.json -d data.json validates one or more data files against a JSON Schema and exits non-zero when any file is invalid.

ajv-cli wraps the Ajv validator to gate config files, API examples, and fixtures against a JSON Schema in CI, so malformed data fails the build instead of production.

What it does

The validate command compiles the schema (-s), loads any referenced schemas (-r), and validates each data file (-d, which accepts globs). It prints per-file valid/invalid and, for invalid files, the failing keyword and instance path.

Common usage

Terminal
ajv validate -s schema.json -d data.json
# validate many files with referenced schemas, draft 2020-12
ajv validate -s schema.json -r "schemas/*.json" \
  -d "fixtures/*.json" --spec=draft2020 --all-errors

Options

FlagWhat it does
-s <file>Schema to validate against
-d <file|glob>Data file(s) to validate
-r <file|glob>Referenced schemas to load
--spec <draft7|draft2019|draft2020>JSON Schema draft
--all-errorsReport every error, not just the first
--errors=<text|json|line>Error output format
-c <module>Load a custom keyword/format module

In CI

Point ajv validate at your committed schema and a glob of data files as a required check; the non-zero exit on any invalid file blocks the PR. Match --spec to the $schema the file declares, or Ajv may reject valid documents as using unknown keywords.

Common errors in CI

A valid file prints data.json valid; an invalid one prints data.json invalid followed by errors like data/name must be string or data must have required property 'id', and the process exits non-zero. "no schema with key or ref ..." means a $ref target was not loaded with -r. "strict mode: unknown keyword" means the schema uses a keyword outside the selected --spec; set the matching draft or --strict=false.

Related guides

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