Jsonschema:唯一标识元素的数组,有序,与其他有约束的元素一起完成

Jsonschema: array of uniquely identified elements, ordered, completed with other elements with constraints

我想为具有这些约束的数据创建一个模式:

->我简化了问题。在实践中,我有超过 2 个对象,并且 "other objects" 在数组的多个位置放置了不同的约束。

我想到了 oneOf 属性,这是我尝试过的:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Schema01",
    "description": "",
    "type": "array",
    "items": {
        "type": "object",
        "oneOf": [
            {   
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "id": {
                        "const": 1
                    },
                    "val01": {
                        "type": "string"
                    }
                },
                "required": [ "id", "val01" ]
            },
            {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "copy_id": {
                        "const": 4
                    },
                    "val01": {
                        "type": "string"
                    }
                },
                "required": [ "copy_id", "val01" ]
            },
            { 
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "id": {
                        "const": 2
                    },
                    "val12": {
                        "type": "string"
                    }
                },
                "required": [ "id", "val12" ]
            }

        ]
    }
}

但是这个模式并不能满足我的所有需求。

它正确验证了这些数据样本: -> 所有必需的对象都在那里(1 和 2),并且有 0 个或更多(这里是 3)个其他元素满足定义的约束,并放置在 id 1 和 2 之间。

[
    { "id": 1, "val01": "object 1" },
    { "copy_id": 4, "val01": "copy object 1" },
    { "copy_id": 4, "val01": "copy object 2" },
    { "copy_id": 4, "val01": "copy object 3" },
    { "id": 2, "val12": "object 2" }
]

[
    { "id": 1, "val01": "object 1" },
    { "id": 2, "val12": "object 2" }
]

它不验证这些数据样本(这是预期的行为),第一个是因为存在不允许的属性,最后一个是因为存在未知元素(id : 3):

[
    { "id": 1, "val01": "object 1", "val02": "object 1" },
    { "id": 2, "val12": "object 2" }
]

[
    { "id": 1, "val01": "object 1" },
    { "id": 2, "val12": "object 2" },
    { "id": 3, "val01": "object 1" }
]

但是它验证了这三个数据样本,所有都应该无效:第一个因为缺少一个(应该是)必需的元素(id:2),第二个因为其中一个必需元素出现了两次 (id: 2),而最后一次是因为顺序不正确(在本例中,copy_id 元素应该出现在 id 1 和 id 2 之间)。

[
    { "id": 1, "val01": "object 1" }
]

[
    { "id": 1, "val01": "object 1" },
    { "id": 2, "val12": "object 2" },
    { "id": 2, "val12": "object 44" }
]

[
    { "id": 1, "val01": "object 1" },
    { "id": 2, "val12": "object 2" },
    { "copy_id": 4, "val01": "copy object 1" },
    { "copy_id": 4, "val01": "copy object 2" },
    { "copy_id": 4, "val01": "copy object 3" }
]

所以(至少?)缺少三个约束:

是否可以做我正在尝试的事情?或者 json-schema 现在不能做这种事情?

一种选择是在验证之前编辑数据,用对象替换主数组,其中id是键,每个对象的剩余部分是值。但我会放宽元素的顺序。

如果可以为前两个缺失的约束找到解决方案而不是第三个(并且不需要编辑数据),我也会很高兴。提前致谢!

So there are (at least?) three constraints missing:

  • One of the required elements (id 1 or id 2) cannot appears two times;
  • Each required elements (id 1 or id 2) should appears in the data;
  • The order of the elements should be important.

好的,让我们一次一个地处理每个需求...

One of the required elements (id 1 or id 2) cannot appears two times;

有一个JSON Schema 关键字uniqueItems,但它只将数组中的每个元素视为一个整体。您无法使用 JSON 架构进行此类数据完整性检查。

Each required elements (id 1 or id 2) should appears in the data;

这是可以做到的,但我怀疑您的要求比看起来的要多。您真的要检查数组是否至少包含一个 ID 为 1 的对象和另一个 ID 为 2 的对象(这些特定值)?还是您的要求比自然界更具动态性? (请发表评论,我很乐意进一步提供帮助。)

The order of the elements should be important.

如果您希望第一个元素的 ID 为 1,第二个元素的 ID 为 2,那是可行的,但看起来您要求允许其他对象出现在中间,这将无法验证。

JSON 架构旨在验证您的 JSON 的结构和格式。听起来您的要求与业务逻辑/应用程序逻辑有关,JSON Schema 通常没有涵盖。这极不可能改变,因此您需要编写代码来检查这些业务逻辑需求。