Command line interface for ajv, one of the fastest json schema validators.
npm install ajv-cli
ajv help
ajv help validate
ajv help compile
ajv help test
This command validates data files against JSON-schema
ajv validate -s test/schema.json -d test/valid_data.json
ajv -s test/schema.json -d test/valid_data.json
You can omit validate
command name and .json
from the input file names.
Only one schema can be passed in this parameter
Multiple data files can be passed, as in -r
parameter:
ajv -s test/schema.json -d "test/valid*.json"
If some file is invalid exit code will be 1.
The schema in -s
parameter can reference any of these schemas with $ref
keyword.
Multiple schemas can be passed both by using this parameter mupltiple times and with glob patterns. Glob pattern should be quoted and extensions cannot be omitted.
Schemas can use any of these schemas as a meta-schema (that is the schema used in $schema
keyword - it is used to validate the schema itself).
Multiple meta-schemas can be passed, as in -r
parameter.
-
--errors=
: error reporting format. Possible values:js
(default): JavaScript objectjson
: JSON with indentation and line-breaksline
: JSON without indentaion/line-breaks (for easy parsing)text
: human readable error messages with data paths
-
--changes=
: detect changes in data after validation.
Data can be modifyed with Ajv options--remove-additional
,--use-defaults
and--coerce-types
).
The changes are reported in JSON-patch format (RFC6902).
Possible values arejs
(default),json
andline
(see--errors
option).
This command validates and compiles schema without validating any data.
It can be used to check that the schema is valid and to create a standalone module exporting validation function (using ajv-pack).
ajv compile -s schema
ajv compile -s schema -o validate.js
Multiple schemas can be passed both by using this parameter mupltiple times and with glob patterns.
ajv compile -s "test/schema*.json"
Only a single schema can be compiled with this option.
ajv compile -s "schema.json" -o "validate_schema.js"
The schema in -s
parameter can reference any of these schemas with $ref
keyword.
Multiple schemas can be passed, as in -s
parameter.
Schemas can use any of these schemas as a meta-schema (that is the schema used in $schema
keyword - it is used to validate the schema itself).
Multiple meta-schemas can be passed, as in -s
parameter.
This command asserts that the result of the validation is as expected.
ajv test -s test/schema.json -d test/valid_data.json --valid
ajv test -s test/schema.json -d test/invalid_data.json --invalid
If the option --valid
(--invalid
) is used for the test
to pass (exit code 0) the data file(s) should be valid (invalid).
This command supports the same options and parameters as validate with the exception of --changes
.
You can pass the following Ajv options:
Option | Description |
---|---|
--v5 |
support v5 proposals |
--all-errors |
collect all errors |
--verbose |
include schema and data in errors |
--json-pointers |
report data paths in errors using JSON-pointers |
--unique-items=false |
do not validate uniqueItems keyword |
--unicode=false |
count unicode pairs as 2 characters |
--format=full |
format mode |
--missing-refs= |
handle missing referenced schemas (true/ignore/fail) |
--remove-additional |
remove additional properties (true/all/failing) |
--use-defaults |
replace missing properties/items with the values from default keyword |
--coerce-types |
change type of data to match type keyword |
--multiple-of-precision |
precision of multipleOf, pass integer number |
--error-data-path=property |
data path in errors |
--messages=false |
do not include text messages in errors |
Options can be passed in either dash-case and camelCase.
See Ajv Options for more information.
See https://github.com/jessedc/ajv-cli/releases
MIT