Warning
JSON Schema Draft 2 and older are not supported at this point in time.
jsonschema validate <schema.json|.yaml> <instance.json|.jsonl|.jsonl.gz|.yaml|directory...>
[--http/-h] [--verbose/-v] [--debug/-g]
[--header/-H "<name>: <value>"]
[--resolve/-r <schemas-or-directories> ...]
[--benchmark/-b] [--loop <iterations>] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--trace/-t] [--fast/-f]
[--template/-m <template.json>] [--json/-j] [--entrypoint/-p <pointer|uri>]
[--continue/-c] [--format-assertion/-F] [--configuration/-C <path>]
[--valid/-V] [--invalid/-I] [--color auto|always|never]Note
See Resolving External References for every way of making referenced schemas available, including how to handle a reference whose URI differs from the identifier the target schema declares.
The most popular use case of JSON Schema is to validate JSON documents. The
JSON Schema CLI offers a validate command to evaluate one or many JSON
instances, directories of instances, or JSONL datasets against a JSON Schema,
presenting human-friendly information on unsuccessful validation.
The --json/-j option outputs the evaluation result using the JSON Schema
Flag or
Basic
standard format depending on whether the --fast/-f option is set.
If you want to validate that a schema adheres to its metaschema, use the
metaschema command instead.
To help scripts distinguish validation errors, these are reported using exit code 2.
Note
The command stops at the first instance that fails validation, whether that
instance is a file, an entry of a JSONL dataset, or one of many arguments.
Pass --continue/-c to report every failing instance instead.
Tip
GZIP-compressed JSONL datasets (.jsonl.gz) are transparently decompressed
during validation. No additional options are needed.
Note
The --trace/-t option is only allowed given a single instance, as its
output carries no per instance header that would tell you which instance a
line belongs to. The --benchmark/-b option is only allowed given a single
file, which may be a JSONL dataset of many instances, as it prints a
<path>[<index>] header for every one of them.
Note
Annotations are reported through the --json/-j output and through the
--trace/-t option. Neither reports them when the --fast/-f option is
passed, as fast mode does not collect annotations.
Warning
By default, schemas are validated in exhaustive mode, which results in better
error messages, at the expense of speed. The --fast/-f option makes the
schema compiler optimise for speed, at the expense of error messages.
Tip
The format keyword is treated as an annotation by default across all
dialects (except 2020-12 with the Format
Assertion
vocabulary enabled). To force format to behave as an assertion, you have
two options. Pass --format-assertion/-F to force every format in the
schema to assert. Alternatively, set the custom x-format-assertion keyword
to true as a sibling to a specific format to force assertion at that
location only. Both mechanisms work across all supported dialects.
For example, the following Draft 4 schema asserts that the instance is a valid email address rather than just annotating it:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "string",
"format": "email",
"x-format-assertion": true
}To speed-up the compilation process, you may pre-compile a schema using the
compile command and pass the result using the
--template/-m option. However, you still need to pass the original schema
for error reporting purposes. Make sure they match and that the compilation and
evaluation were done with the same version of this tool or you might get
non-sense results.
Tip
Templates produced by the compile command can
also be evaluated from JavaScript using the Blaze JavaScript
port, a
pure-JavaScript evaluator for browsers and JavaScript runtimes like
Node.js, letting you compile with this CLI and validate anywhere.
For example, consider the following JSON Schema Draft 4 schema that asserts that the JSON instance is a string:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "string"
}Also consider a JSON instance called instance.json that looks like this:
12345This instance is an integer, while the given schema expects a string. Validating the instance against the schema using the JSON Schema CLI will result in the following output:
$ jsonschema validate schema.json instance.json
error: The target document is expected to be of the given type
at instance location ""
at evaluate path "/type"jsonschema validate path/to/my/schema.json path/to/my/instance.jsonjsonschema validate path/to/my/schema.json path/to/my/instance.json --jsonjsonschema compile path/to/my/schema.json > template.json
jsonschema validate path/to/my/schema.json path/to/my/instance.json \
--template template.jsonjsonschema validate path/to/my/schema.json path/to/my/instance.json --fastjsonschema validate path/to/my/schema.json path/to/my/instance.json --format-assertionjsonschema validate path/to/my/schema.json \
path/to/my/instance_1.json \
path/to/my/instance_2.json \
path/to/my/instance_3.jsonjsonschema validate path/to/my/schema.json path/to/my/dataset.jsonljsonschema validate path/to/my/schema.json path/to/my/dataset.jsonl.gzjsonschema validate path/to/my/schema.json path/to/my/dataset.jsonl --continueNote that even with --continue, only the first error of each failing instance
is reported. Continuing validation past the first error within a single instance
is a gray area not covered by the JSON Schema specification and potentially
tricky to implement correctly at the evaluation level.
jsonschema validate path/to/my/schema.json path/to/my/instance.json --httpjsonschema validate path/to/my/schema.json path/to/my/instance.json \
--http --header "Authorization: Bearer $REGISTRY_TOKEN"jsonschema validate path/to/my/schema.json path/to/my/instance.json \
--resolve path/to/external.jsonjsonschema validate path/to/my/schema.json path/to/my/instance.json \
--resolve path/to/schemas --extension schema.jsonjsonschema validate path/to/my/schema.json path/to/my/instance.json --benchmarkjsonschema validate path/to/my/schema.json path/to/my/instance.json --tracejsonschema validate path/to/my/schema.json path/to/instances/jsonschema validate path/to/my/schema.json path/to/instances/ --extension .data.jsonjsonschema validate path/to/my/schema.json path/to/instances/ \
--ignore path/to/instances/draftsjsonschema validate path/to/my/schema.json path/to/my/instance.json --invalidjsonschema validate path/to/my/schema.json path/to/invalid-instances/ \
--invalid --continuejsonschema validate path/to/my/schema.json path/to/my/instance.json \
--entrypoint '/$defs/MyType'