我有一个无序的JSON项array.根据规范https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-03#section-5.5,下面的JSON模式将仅验证数组中的对象是否以该顺序出现.我不想指定顺序,只需验证数组中的对象,而不考虑对象的顺序或数量.从说明书上看,我似乎不明白这是怎么做的.

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

推荐答案

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:

你好,

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

https://datatracker.ietf.org/doc/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.

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"}
        ]
    }
}

这是正确的答案.现在,我更正的模式包括:

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

Json相关问答推荐

Google Page to JSON应用程序脚本出现CORS错误

使用Jolt将字符串数组转换为JSON对象数组

解析JSON说函数parse_json不存在?

Flutter -控制器问题-JSON API

使用自定义类型在Golang中解析JSON数组

将部分数据字节解组到自定义 struct 中

使用 jq 重新格式化 JSON 输出

PostgreSQL:删除 JSONB 数组中每个元素的特定键

Jolt 变换以展平 json 字符串数组

迭代powershell双维json对象

具有 (RegEx) 模式的 json-schema 中的枚举

使用 jq 和脚本 bash 映射两个 json

如何使用nifi从json文件中过滤属性

为什么根据其他工具,来自 aws rds 的 JSON 在 Docker 中格式错误运行?

坚持弄清楚如何使用 api 响应来调用以从不同的链接检索响应

为什么在测试 RSPEC 时 JBuilder 不返回 JSON 中的响应正文

规范化 JSON 文件

将错误消息作为 JSON 对象发送

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

杰克逊自动将 Joda DateTime 格式化为 ISO 8601 格式