简体   繁体   English

如果 json 有多个数据集,我如何编写 json 模式

[英]How can i write the json schema if json has multiple data set

I am new to this json schema , i am able to write json schema if it has only one data set like below我是这个 json 模式的新手,如果它只有一个数据集,我可以编写 json 模式,如下所示

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

example json-schema for this is示例 json-schema 是

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

but i got stuck to write json schema in ruby if we have multiple data sets但是如果我们有多个数据集,我就会坚持在 ruby​​ 中编写 json 模式

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

can anyone please help me to write json schema if it has multiple data sets for same schema to validate the response body如果有多个数据集用于同一架构来验证响应正文,任何人都可以帮我编写 json 架构吗?

Here is the schema you are looking for.这是您正在寻找的架构。

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

You were really close.你真的很亲近。 The items keyword has two forms. items关键字有两种形式。 The value of items can be a schema or an array of schemas(1). items的值可以是模式或模式数组(1)。

If items is a schema, it means that every item in the array must conform to that schema.如果items是架构,则意味着数组中的每个项目都必须符合该架构。 This is the form that is useful in this case.这是在这种情况下有用的表格。

If the value of items is an array of schemas, it describes a tuple.如果items的值是一个架构数组,则它描述一个元组。 For example, this schema ...例如,这个架构...

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

would validate this ...将验证这一点...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM