如果json有多个数据集,我该如何写json模式 [英] How can i write the json schema if json has multiple data set

查看:349
本文介绍了如果json有多个数据集,我该如何写json模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个json模式的新手,如果它只有一个像下面这样的数据集,我就可以编写json模式

I am new to this json schema , i am able to write json schema if it has only one data set like below

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

示例json-schema

example json-schema for this is

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

但是如果我们有多个数据集,我就无法在ruby中编写json模式

but i got stuck to write json schema in ruby if we have multiple data sets

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

任何人都可以帮助我编写json模式,如果它具有用于同一模式的多个数据集以验证响应正文的话

can anyone please help me to write json schema if it has multiple data sets for same schema to validate the response body

推荐答案

这是您要查找的架构.

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

您真的很亲近. items关键字有两种形式. items的值可以是模式或模式数组(1).

You were really close. The items keyword has two forms. The value of items can be a schema or an array of schemas(1).

如果items是架构,则意味着数组中的每个项目都必须符合该架构.这是在这种情况下有用的表格.

If items is a schema, it means that every item in the array must conform to that schema. This is the form that is useful in this case.

如果items的值是一个模式数组,则它描述一个元组.例如,此架构...

If the value of items is an array of schemas, it describes a tuple. 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

这篇关于如果json有多个数据集,我该如何写json模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆