v8r
v8r is a command-line JSON and YAML validator that uses Schema Store to detect a suitable schema for your input files based on the filename.
Getting Started
One-off:
npx v8r@latest <filename>
Local install:
npm install -g v8r
v8r <filename>
Usage Examples
Validating files
v8r can validate JSON or YAML files. You can pass filenames or glob patterns:
$ v8r package.json
$ v8r file1.json file2.json
$ v8r 'dir/*.yml' 'dir/*.yaml'
DigitalOcean's Glob Tool can be used to help construct glob patterns
Manually specifying a schema
By default, v8r queries Schema Store to detect a suitable schema based on the filename.
$ v8r feature.geojson
✖ Could not find a schema to validate feature.geojson
$ v8r feature.geojson --schema https://json.schemastore.org/geojson
ℹ Validating feature.geojson against schema from https://json.schemastore.org/geojson ...
✔ feature.geojson is valid
Using a custom catlog
Using the --schema
flag will validate all files matched by the glob pattern against that schema. You can also define a custom schema catalog. v8r will search any custom catalogs before falling back to Schema Store.
$ cat > my-catalog.json <<EOF
{ "\$schema": "https://json.schemastore.org/schema-catalog.json",
"version": 1,
"schemas": [ { "name": "geojson",
"description": "geojson",
"url": "https://json.schemastore.org/geojson.json",
"fileMatch": ["*.geojson"] } ] }
EOF
$ v8r feature.geojson -c my-catalog.json
ℹ Found schema in my-catalog.json ...
ℹ Validating feature.geojson against schema from https://json.schemastore.org/geojson ...
✔ feature.geojson is valid
This can be used to specify different custom schemas for multiple file patterns.
Configuration
v8r uses CosmiConfig to search for a configuration. This means you can specify your configuration in any of the following places:
package.json
.v8rrc
.v8rrc.json
.v8rrc.yaml
.v8rrc.yml
.v8rrc.js
.v8rrc.cjs
v8r.config.js
v8r.config.cjs
v8r only searches for a config file in the current working directory.
Example yaml config file:
patterns: ['*json']
verbose: 2
ignoreErrors: true
cacheTtl: 86400
format: "json"
customCatalog:
schemas:
- name: Custom Schema
description: Custom Schema
fileMatch: ["*.geojson"]
location: foo/bar/geojson-schema.json
parser: json5
The config file format is specified more formally in a JSON Schema:
Exit codes
-
v8r always exits with code 0
when:
- The input glob pattern(s) matched one or more files, all input files were validated against a schema, and all input files were valid
v8r
was called with --help
or --version
flags
-
By default v8r exits with code 1
when an error was encountered trying to validate one or more input files. For example:
- No suitable schema could be found
- An error was encountered during an HTTP request
- An input file was not JSON or yaml
- etc
This behaviour can be modified using the --ignore-errors
flag. When invoked with --ignore-errors
v8r will exit with code 0
even if one of these errors was encountered while attempting validation. A non-zero exit code will only be issued if validation could be completed successfully and the file was invalid.
-
v8r always exits with code 97
when:
- There was an error loading a config file
- A config file was loaded but failed validation
-
v8r always exits with code 98
when:
- An input glob pattern was invalid
- An input glob pattern was valid but did not match any files
-
v8r always exits with code 99
when:
- The input glob pattern matched one or more files, one or more input files were validated against a schema and the input file was invalid
Versioning
v8r follows semantic versioning. For this project, the "API" is defined as:
- CLI flags and options
- CLI exit codes
- The configuration file format
- The native JSON output format
A "breaking change" also includes:
- Dropping compatibility with a major Node JS version
- Dropping support for a JSON Schema draft
FAQ
❓ How does v8r
decide what schema to validate against if I don't supply one?
💡 v8r
queries the Schema Store catalog to try and find a suitable schema based on the name of the input file.
❓ My file is valid, but it doesn't validate against one of the suggested schemas.
💡 v8r
is a fairly thin layer of glue between Schema Store (where the schemas come from) and ajv (the validation engine). It is likely that this kind of problem is either an issue with the schema or validation engine.
❓ What JSON schema versions are supported?
💡 v8r
works with JSON schema drafts:
- draft-04
- draft-06
- draft-07
- draft 2019-09
- draft 2020-12
❓ Will 100% of the schemas on schemastore.org work with this tool?
💡 No. There are some with known issues
❓ Can v8r
validate against a local schema?
💡 Yes. The --schema
flag can be either a path to a local file or a URL. You can also use a config file to include local schemas in a custom catalog.