【发布时间】:2015-09-04 05:36:19
【问题描述】:
我想描述一个具有数组类型属性的对象的模式。该数组中的项目必须属于同一类型。但是对于该数组中的项目,两个不同的对象可以有不同的类型:
// 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
}
但是有很多相同数组属性的重复项。 有没有办法调整第二个模式以避免重复?或者有什么其他建议?
【问题讨论】:
标签: json jsonschema