jsonschema:对象数组中的唯一属性
jsonschema: Unique Properties in an Object Array
我有一个带有数组的模式,我想确保数组中的一个 属性 对于数组中所有其他相同的属性都是唯一的。使用 uniqueItems
只能确保整个对象是唯一的,而不是特定的 属性.
举个例子会让这个问题更清楚。在下面的数组中,我希望 key
在数组中是唯一的,但 content
不一定是。我将如何制作 json 架构,以便 good_array
以下通过但 bad_array
失败?
good_array = [
{"key":1, "content":"foo"},
{"key":2, "content":"bar"},
{"key":3, "content":"foo"}
]
bad_array = [
{"key":1, "content":"foo"},
{"key":1, "content":"bar"},
{"key":3, "content":"foo"}
]
JSON 架构无法做到这一点。对不起。
Two JSON instances are said to be equal if and only if they are of
the same type and have the same value according to the data model.
Specifically, this means:
both are null; or
both are true; or
both are false; or
both are strings, and are the same codepoint-for-codepoint; or
both are numbers, and have the same mathematical value; or
both are arrays, and have an equal value item-for-item; or
both are objects, and each property in one has exactly one
property with a key equal to the other's, and that other property
has an equal value.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-4.2.3
由于 JSON 模式标准不支持这一点,我创建了一个 Python 包 (JsonVL),它支持对数组中对象中的值的唯一约束。
可以用pip install jsonvl
安装
然后 运行 与:
from jsonvl import validate
validate(data, schema)
GitHub 存储库中的代码示例:https://github.com/gregorybchris/jsonvl
我有一个带有数组的模式,我想确保数组中的一个 属性 对于数组中所有其他相同的属性都是唯一的。使用 uniqueItems
只能确保整个对象是唯一的,而不是特定的 属性.
举个例子会让这个问题更清楚。在下面的数组中,我希望 key
在数组中是唯一的,但 content
不一定是。我将如何制作 json 架构,以便 good_array
以下通过但 bad_array
失败?
good_array = [
{"key":1, "content":"foo"},
{"key":2, "content":"bar"},
{"key":3, "content":"foo"}
]
bad_array = [
{"key":1, "content":"foo"},
{"key":1, "content":"bar"},
{"key":3, "content":"foo"}
]
JSON 架构无法做到这一点。对不起。
Two JSON instances are said to be equal if and only if they are of
the same type and have the same value according to the data model.
Specifically, this means:both are null; or both are true; or both are false; or both are strings, and are the same codepoint-for-codepoint; or both are numbers, and have the same mathematical value; or both are arrays, and have an equal value item-for-item; or both are objects, and each property in one has exactly one property with a key equal to the other's, and that other property has an equal value.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-4.2.3
由于 JSON 模式标准不支持这一点,我创建了一个 Python 包 (JsonVL),它支持对数组中对象中的值的唯一约束。
可以用pip install jsonvl
然后 运行 与:
from jsonvl import validate
validate(data, schema)
GitHub 存储库中的代码示例:https://github.com/gregorybchris/jsonvl