正确的JSON模式用于不同类型的项目数组 [英] Correct JSON Schema for an array of items of different type

查看:266
本文介绍了正确的JSON模式用于不同类型的项目数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无序的JSON项数组.根据规范 http://tools.ietf.org /html/draft-zyp-json-schema-03#section-5.5 下面的json模式仅在数组中的对象出现在那个顺序中时才会验证.我不想指定顺序,只验证数组中的对象,而不管对象的顺序或数量如何.从规格上看,我似乎无法理解其完成方式.

I have an unordered array of JSON items. According to the specification http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.5 the json schema below will only validate if the objects in the array appear IN THAT ORDER. I don't want to specify an order, just validate the objects within the array, regardless of order or number of objects. From the spec I can't seem to understand how this is done.

"transactions" : {
    "type" : "array",
    "items" : [
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BUILD", "REASSIGN"]
                }
            }
        },
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BREAK"]
                }
            }
        }
    ]
}

推荐答案

我在JSON模式google组上问了同样的问题,并很快得到了答复. fge用户要求我在此处发布他的回复:

I asked this same question on the JSON schema google group, and it was answered quickly. User fge asked that I post his response here:

你好,

当前规范是v4草案,而不是v3草案.更多的 具体来说,验证规范在这里:

The current specification is draft v4, not draft v3. More specifically, the validation specification is here:

http://tools.ietf.org/html/draft-fge-json-schema-validation-00

该网站不是最新的,我不知道为什么...我将提交一份说明 要求.

The web site is not up to date, I don't know why... I'll submit a pull request.

在草案v4中,您可以使用以下代码:

With draft v4 you can use this:

{
    "type": "array",
    "items": {
        "oneOf": [
            {"first": [ "schema", "here" ] }, 
            {"other": [ "schema": "here" ] }
        ]
    }  
}

例如,这是一个数组的架构,其中项目可以是 字符串或整数(尽管可以用更简单的方式编写):

For instance, this is a schema for an array where items can be either strings or integers (it can be written in a more simple way though):

{
    "type": "array",
    "items": {
        "oneOf": [
            {"type": "string"},
            {"type": "integer"}
        ]
    }
}

这是正确的答案.我更正后的架构现在包括:

This is the correct answer. My corrected schema now includes:

"transactions" : {
    "type" : "array",
    "items" : {
        "oneOf" : [
            {
                "type" : "object",
                "properties" : {
                    "type" : {
                        "type" : "string",
                        "enum" : ["BUILD", "REASSIGN"]
                    }
                }
            },
            {
               "type" : "object",
               "properties" : {
                 "type" : {
                   "type" : "string",
                   "enum" : ["BREAK"]
                  }
               }
            }
        ]
    }
}

这篇关于正确的JSON模式用于不同类型的项目数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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