【问题标题】:How can i write the json schema if json has multiple data set如果 json 有多个数据集,我该如何编写 json 架构
【发布时间】:2015-09-23 04:22:57
【问题描述】:

我是这个 json 架构的新手,如果它只有一个如下所示的数据集,我可以编写 json 架构

{
    "employees": [
        {
            "id": 1,
            "name": "aaa"
        }
}

示例 json-schema 是

{   
        "type" : "object",
            "required" : ["employees"],
            "properties" : {        
                "employees" : { "type" : "Array",
                                "items" : [
                                "properties" : {
                                                    "id" : {"type" : "integer"},
                                                    "name" : {"type" : "string"},                                                   
                                                },
                                            "required" : ["id","name"]
                                            ]
                                }
                            }
}

但是如果我们有多个数据集,我会被困在 ruby​​ 中编写 json 模式

{
    "employees": [
        {
            "id": 1,
            "name": "aaa"
        },
        {
            "id": 2,
            "name": "bbb"
        },
        {
            "id": 3,
            "name": "cccc"
        },
        {
            "id": 4,
            "name": "ddd"
        },
        {
            "id": 5,
            "name": "eeee"
        }
    ]
}

如果同一模式有多个数据集来验证响应正文,任何人都可以帮我编写 json 模式

【问题讨论】:

  • 可以写hash,然后转成json
  • 但是员工数据集的数量总是不一样的,如果他们在数据库中插入新记录会增加

标签: ruby json jsonschema


【解决方案1】:

这是您要查找的架构。

{
  "type": "object",
  "required": ["employees"],
  "properties": {
    "employees": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "name": { "type": "string" }
        },
        "required": ["id", "name"]
      }
    }
  }
}

你真的很亲密。 items 关键字有两种形式。 items 的值可以是架构或架构数组(1)。

如果items 是一个模式,则意味着数组中的每个项目都必须符合该模式。这是在这种情况下有用的形式。

如果items 的值是一个模式数组,它描述了一个元组。比如这个架构...

{
  "type": "array",
  "items": [
    { "type": "boolean" },
    { "type": "string" }
  ]
}

会验证这个...

[true, "foo"]
  1. http://json-schema.org/latest/json-schema-validation.html#anchor37

【讨论】:

  • 啊。你远远领先于我。我发现正确的嵌套非常耗时。
  • @json 非常感谢帮助我的老兄
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 2015-08-27
相关资源
最近更新 更多