Search code examples
jsoncommand-line-interfacejsonschemapython-jsonschema

How to validate json schema against metaschema in linux cli


I'm trying to validate my complex json schema definition file to be sure that there is no typo in the schema. I use the jsonschema script provided by python jsonschema library.

I use meta-schema files downloaded from json schema specification page.

I was downloaded the "Core/Validation Dialect meta-schema" file and all "Single-vocabulary meta-schemas", added the "json" extension and store these files in this structure:

├── meta
│   ├── applicator.json
│   ├── content.json
│   ├── core.json
│   ├── format-annotation.json
│   ├── format-assertion.json
│   ├── meta-data.json
│   ├── unevaluated.json
│   └── validation.json
└── schema.json

If I create this test01.json file (notice the "objectx" typo on line 5):

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/sample/conf.schema.json",
  "title": "Sample",
  "type": "objectx",
  "properties": {
    "browser": {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string"
          }
        }
    }
  }
}

then the validation fail (as expected):

$ jsonschema -i test01.json some/path/schema.json
objectx: 'objectx' is not valid under any of the given schemas

But when I make similar typo in embedded object (see line 8) in test02.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/sample/conf.schema.json",
  "title": "Sample",
  "type": "object",
  "properties": {
    "browser": {
      "type": "objectx",
      "properties": {
        "foo": {
          "type": "string"
          }
        }
    }
  }
}

then the validation pass (no output is printed):

$ jsonschema -i test02.json some/path/schema.json

How can I validate complex json schema document (not only the top level object) from CLI in linux?


Solution

  • The CLI in jsonschema is deprecated in these days.

    It is recommended to use check-jsonschema instead. It has a --check-metaschema option. This means that it has embedded meta schema files, so there is no need to download meta schema manually anymore. That's good news.

    The second good news is, that it just works and it is able to validate complex schema documents:

    $ check-jsonschema --check-metaschema test02.json 
    Schema validation errors were encountered.
      test02.json::$.properties.browser.type: 'objectx' is not valid under any of the given schemas
      Underlying errors caused this.
      Best Match:
        $.properties.browser.type: 'objectx' is not one of ['array', 'boolean', 'integer', 'null', 'number', 'object', 'string']