当此类型在不同对象中不同时,JSON 对象的架构具有相同类型的项目数组
Schema for JSON object with an array of items of the same type when this type is different in different objects
我想描述具有 属性 数组类型的对象的架构。该数组中的项目必须属于同一类型。但是两个不同的对象对于该数组中的项目可以具有不同的类型:
// object_1
{
<...>,
"array_of_some_type": [1, 2, 3, 4, 5],
<...>
}
// object_2
{
<...>,
"array_of_some_type": ["one", "two", "three"],
<...>
}
我尝试使用 oneOf
关键字:
{
"type": "object",
"properties": {
<...>
"array_of_some_type": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"oneOf": [
{ "type": "number" },
{ "type": "string" },
{ "type": "object" }
]
},
"additionalItems": false
},
<...>
},
"required": [ "array_of_some_type" ],
"additionalProperties": false
}
但这是错误的,因为此模式在我的案例对象中有效无效:
// invalid_object
{
<...>,
"array_of_some_type": [1, "two", 3],
<...>
}
正确的架构可能如下所示:
{
"type": "object",
"properties": {
<...>
"array_of_some_type": {
"oneOf": [
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "type": "number" },
"additionalItems": false
},
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "type": "string" },
"additionalItems": false
},
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "type": "object" },
"additionalItems": false
}
]
},
<...>
},
"required": [ "array_of_some_type" ],
"additionalProperties": false
}
但是有很多相同数组属性的重复项。
有没有办法调整第二个模式以避免重复?或者还有其他建议吗?
您只能将变化的部分放在 oneOf
子句中。我认为 JSON-Schema 必须支持类似 Java 的泛型才能更清晰地表达这一点。
{
"type": "object",
"properties": {
"array_of_some_type": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"oneOf": [
{ "items": { "type": "number" } },
{ "items": { "type": "string" } },
{ "items": { "type": "object" } }
]
}
},
"required": [ "array_of_some_type" ],
"additionalProperties": false
}
我想描述具有 属性 数组类型的对象的架构。该数组中的项目必须属于同一类型。但是两个不同的对象对于该数组中的项目可以具有不同的类型:
// object_1
{
<...>,
"array_of_some_type": [1, 2, 3, 4, 5],
<...>
}
// object_2
{
<...>,
"array_of_some_type": ["one", "two", "three"],
<...>
}
我尝试使用 oneOf
关键字:
{
"type": "object",
"properties": {
<...>
"array_of_some_type": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"oneOf": [
{ "type": "number" },
{ "type": "string" },
{ "type": "object" }
]
},
"additionalItems": false
},
<...>
},
"required": [ "array_of_some_type" ],
"additionalProperties": false
}
但这是错误的,因为此模式在我的案例对象中有效无效:
// invalid_object
{
<...>,
"array_of_some_type": [1, "two", 3],
<...>
}
正确的架构可能如下所示:
{
"type": "object",
"properties": {
<...>
"array_of_some_type": {
"oneOf": [
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "type": "number" },
"additionalItems": false
},
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "type": "string" },
"additionalItems": false
},
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": { "type": "object" },
"additionalItems": false
}
]
},
<...>
},
"required": [ "array_of_some_type" ],
"additionalProperties": false
}
但是有很多相同数组属性的重复项。 有没有办法调整第二个模式以避免重复?或者还有其他建议吗?
您只能将变化的部分放在 oneOf
子句中。我认为 JSON-Schema 必须支持类似 Java 的泛型才能更清晰地表达这一点。
{
"type": "object",
"properties": {
"array_of_some_type": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"oneOf": [
{ "items": { "type": "number" } },
{ "items": { "type": "string" } },
{ "items": { "type": "object" } }
]
}
},
"required": [ "array_of_some_type" ],
"additionalProperties": false
}