【问题标题】:Json schema ref other fileJson 模式参考其他文件
【发布时间】:2017-05-21 00:23:14
【问题描述】:

使用此架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "b": {
    "type": "object",
    "properties": {
      "c": {
        "type": "string"
      }
    }
  },
  "type": "object",
  "properties": {
    "a": {
      "$ref": "#/b"
    }
  }
}

我可以验证这个例子:

{
  "a": {
    "c": "test"
  }
}

现在我想为“b”元素创建一个新的架构文件,并在我的第一个架构中引用它。我怎样才能做到这一点 ?我尝试了很多东西,但我总是得到 jsonspec.reference.exceptions.NotFound: u'b.json' not registered。

【问题讨论】:

    标签: json file schema ref


    【解决方案1】:

    经过几个小时的谷歌搜索和各种文档阅读后,我终于能够使用"$ref" : "file:..." 编写两个 json-schema。

    我的示例数据如下所示:

    john-doe.json:

    {
      "first_name": "john",
      "last_name": "doe",
      "age": 42,
      "address": {
        "street_address": "foo street 42",
        "city": "baklavastan",
        "state": "foobar"
        "foo": 42
      }
    }
    

    然后我有两个 json 架构文件,它们位于我的文件系统的同一目录中。

    customer.json

    {
      "type": "object",
      "properties": {
        "first_name": { "type": "string" },
        "last_name":  { "type": "string" },
        "age" :       { "type" : "integer" },
        "address" :   { "$ref" : "file:address.json#" }
      },
      "required": ["first_name", "last_name", "age", "address"],
      "additionalProperties": false
    }
    

    address.json

    {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"],
      "additionalProperties": false
    }
    

    我能够使用库 scjsv(它是 Java json-schema-validation 库的包装器)针对 Clojure 中的架构进行验证

    它给了我以下 john-doe.json 示例的验证错误(在 clojure edn 中):

    { :level "error",
      :schema {:loadingURI "file:address.json#", :pointer ""},
      :instance {:pointer "/address"},
      :domain "validation",
      :keyword "additionalProperties",
      :message
      "object instance has properties which are not allowed by the schema: [\"foo\"]",
      :unwanted ["foo"] }
    

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 2015-10-24
      • 2018-02-01
      • 2022-09-28
      • 2020-04-17
      • 2022-01-22
      • 2018-03-24
      • 2012-02-23
      相关资源
      最近更新 更多