如何在 Linux cli 中根据元模式验证 json 模式

eNc*_*Nca 5 json command-line-interface jsonschema python-jsonschema

我正在尝试验证我的复杂json 架构定义文件,以确保架构中没有拼写错误。\n我使用python jsonschema 库jsonschema提供的脚本。

\n

我使用从json 架构规范页面下载的元架构文件。

\n

我下载了“核心/验证方言元模式”文件和所有“单词汇元模式”,添加了“json”扩展名并将这些文件存储在以下结构中:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 applicator.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 content.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-annotation.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-assertion.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta-data.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 unevaluated.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 validation.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 schema.json\n
Run Code Online (Sandbox Code Playgroud)\n

如果我创建此test01.json文件(请注意第 5 行的“objectx”拼写错误):

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 applicator.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 content.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-annotation.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-assertion.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta-data.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 unevaluated.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 validation.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 schema.json\n
Run Code Online (Sandbox Code Playgroud)\n

然后验证失败(如预期):

\n
$ jsonschema -i test01.json some/path/schema.json\nobjectx: \'objectx\' is not valid under any of the given schemas\n
Run Code Online (Sandbox Code Playgroud)\n

但是当我在嵌入对象中犯了类似的拼写错误(参见第 8 行)时test02.json

\n
{\n  "$schema": "https://json-schema.org/draft/2020-12/schema",\n  "$id": "https://example.com/sample/conf.schema.json",\n  "title": "Sample",\n  "type": "objectx",\n  "properties": {\n    "browser": {\n      "type": "object",\n      "properties": {\n        "foo": {\n          "type": "string"\n          }\n        }\n    }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后验证通过(不打印输出):

\n
$ jsonschema -i test02.json some/path/schema.json\n
Run Code Online (Sandbox Code Playgroud)\n

如何从 Linux 中的 CLI 验证复杂的 json 模式文档(不仅仅是顶级对象)?

\n

eNc*_*Nca 0

如今,jsonschema中的 CLI已被弃用。

建议使用check-jsonschema代替。它有一个--check-metaschema选项。这意味着它已经嵌入了元模式文件,因此无需再手动下载元模式。那是好消息。

第二个好消息是,它可以正常工作并且能够验证复杂的模式文档:

$ 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']

Run Code Online (Sandbox Code Playgroud)