Use 'Another Json Validator' (ajv) from the command line
Command line interface for ajv, one of the fastest json schema validators. Supports JSON, JSON5, and YAML.
npm install -g ajv-cli
Parameter
--speccan be used with all commands (other than help) to define which JSON schema version is used:
--spec=draft7(default) - support JSON Schema draft-07 (uses
import Ajv from "ajv")
--spec=draft2019- support JSON Schema draft-2019-09 (uses
import Ajv from "ajv/dist/2019")
ajv help ajv help validate ajv help compile ajv help migrate 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
validatecommand name and
.jsonfrom the input file names.
-s- file name of JSON-schema
Only one schema can be passed in this parameter
-d- JSON data
Multiple data files can be passed, as in
-rparameter:
ajv -s test/schema.json -d "test/valid*.json"
If some file is invalid exit code will be 1.
-r- referenced schemas
The schema in
-sparameter can reference any of these schemas with
$refkeyword.
Multiple schemas can be passed both by using this parameter multiple times and with glob patterns. Glob pattern should be quoted and extensions cannot be omitted.
-m- meta-schemas
Schemas can use any of these schemas as a meta-schema (that is the schema used in
$schemakeyword - it is used to validate the schema itself).
Multiple meta-schemas can be passed, as in
-rparameter.
-c- custom keywords/formats definitions
You can pass module(s) that define custom keywords/formats. The modules should export a function that accepts Ajv instance as a parameter. The file name should start with ".", it will be resolved relative to the current folder. The package name can also be passed - it will be used in require as is.
For example, you can use
-c ajv-keywordsto add all keywords from ajv-keywords package or
-c ajv-keywords/keywords/typeofto add only typeof keyword.
--errors=: error reporting format. Possible values:
js(default): JavaScript object
json: JSON with indentation and line-breaks
line: JSON without indentation/line-breaks (for easy parsing)
text: human readable error messages with data paths
--changes=: detect changes in data after validation.
--remove-additional,
--use-defaultsand
--coerce-types).
js(default),
jsonand
line(see
--errorsoption).
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 schemacompile to module file
ajv compile -s schema -o validate.js
compile to stdout, to allow code formatting (js-beautify has to be installed separately)
ajv compile -s schema -o | js-beautify > validate.js
-s- file name(s) of JSON-schema(s)
Multiple schemas can be passed both by using this parameter multiple times and with glob patterns.
ajv compile -s "test/schema*.json"
-o- output file for compiled validation function module
If multiple schemas are compiled with this option the module will have multiple exports named as schema $id's or as file names, otherwise the module will export validation function as default export.
ajv compile -s "schema.json" -o "validate_schema.js"
-owithout parameter should be used to output code to stdout to pass it to some code formatter.
This command also supports parameters
-r,
-mand
-cas in validate command.
This command validates and migrates schema from JSON Schema draft-04 to JSON Schema draft-07 or draft-2019-09 using json-schema-migrate package.
The version of JSON Schema is determined by
--specparameter (
"draft7"or
"draft2019").
ajv migrate -s schemacompile to specific file name
ajv migrate -s schema -o migrated_schema.json
-s- file name(s) of JSON-schema(s)
Multiple schemas can be passed both by using this parameter multiple times and with glob patterns.
ajv migrate -s "test/schema*.json"
If parameter
-ois not specified the migrated schema is written to the same file and the original file is preserved with
.bakextension.
If migration doesn't change anything in the schema file no changes in files are made.
-o- output file for migrated schema
Only a single schema can be migrated with this option.
ajv compile -s "schema.json" -o migrated_schema.json
v5: migrate schema as v5 if $schema is not specified
--indent=: indentation in migrated schema JSON file, 4 by default
--validate-schema=false: skip schema validation
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
testto 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 | | ---------------------------- | ------------------------------------------------------------------------- | | Strict mode | |
--strict=false| disable strict mode | |
--strict-tuples=| throw on (
true) or ignore (
false) strict tuples restrictions (the default is to log) | |
--strict-types=| throw on (
true) or ignore (
false) strict types restrictions (the default is to log) | |
--allow-matching-properties| allow
propertiesmatching patterns in
patternProperties| |
--allow-union-types| allow union types | |
--validate-formats=false| disable format validation | | Validation and reporting | |
--data| use $data references | |
--all-errors| collect all validation errors | |
--verbose| include schema and data in errors | |
--comment| log schema
$comments | |
--inline-refs=| referenced schemas compilation mode (true/false/<number>) | | Modify validated data | |
--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 | | Advanced | |
--multiple-of-precision| precision of multipleOf, pass integer number | |
--messages=false| do not include text messages in errors | |
--loop-required=| max size of
requiredto compile to expression (rather than to loop) | |
--loop-enum=| max size of
enumto compile to expression (rather than to loop) | |
--own-properties| only validate own properties (not relevant for JSON, but can have effect for JavaScript objects) | | Code generation | |
--code-es5| generate ES5 code | |
--code-lines| generate multi-line code | |
--code-optimize=| disable optimization (
false) or number of optimization passes (1 pass by default) | |
--code-formats=| code to require formats object (only needed if you generate standalone code and do not use ajv-formats) |
Options can be passed using either dash-case or camelCase.
See Ajv Options for more information.
See https://github.com/jessedc/ajv-cli/releases