jsonschema lint [schemas-or-directories...]
[--fix/-f] [--verbose/-v] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>]
JSON Schema is a surprisingly expressive schema language. Like with traditional
programming languages, writing efficient and maintainable schemas takes
experience, and there are lots of common pitfalls. Just like popular linters
like ESLint,
ClangTidy, and
PyLint, the JSON Schema CLI provides a lint
command
that can check your schemas against various common anti-patterns and
automatically fix many of them.
Tip
In comparison to Spectral, this CLI is exclusively focused on linting JSON Schema whereas Spectral focuses on linting API specifications (OpenAPI, AsyncAPI, Arazzo, etc) touching on JSON Schema as a byproduct. Therefore, this CLI is expected to have deeper coverage of JSON Schema and be also usable in JSON Schema use cases that are not related to APIs. If you are working with JSON Schema for API specifications, you should make use of both linters together!
For example, consider the following schema that asserts that the JSON instance
is the string foo
:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "foo" ]
}
While this schema is technically correct, the JSON Schema 2020-12 dialect has a
const
keyword
that better expresses the intention of matching a single possible value.
Running the JSON Schema CLI linter against the previous schema will produce the following output:
$ jsonschema lint schema.json
schema.json
An `enum` of a single value can be expressed as `const` (enum_to_const)
Furthermore, running the lint
command with the --fix / -f
option will
result in the JSON Schema CLI automatically fixing the warning for you.
jsonschema lint path/to/my/schema_1.json path/to/my/schema_2.json
jsonschema lint path/to/schemas/
jsonschema lint path/to/schemas/ --ignore path/to/schemas/nested
jsonschema lint
jsonschema lint --extension .schema.json
jsonschema lint path/to/my/schema.json --fix