我的问题陈述是: 考虑一个包含15行的列表,所有行应具有5个键。但是,只有第0行将具有4个键。但是其余所有行将具有所有5个键。
我想再次验证我的回答。是否确实存在first和other关键字。
我在这里Correct JSON Schema for an array of items of different type
示例架构
{
"type": "array",
"items": {
"oneOf": [
{
"first": [{
"type": "object",
"required": ["state"],
"properties":{
"state":{
"type":"string"
}
}
}]
},
{
"other": [{
"type": "object",
"required": ["state", "zip"],
"properties":{
"state":{
"type":"string"
},
"zip":{
"type":"string"
}
}
}]
}
]
}
}
答案 0 :(得分:0)
首先,要实现以下架构定义要实现什么目标?
"first" : [ { ...schema... } ]
关于您的问题陈述,我不确定您要实现的目标:
该模式允许第一个数组项成为具有4个键的对象,而其他所有项都应具有5个键?
模式,该模式仅允许使用5个键的array items = object数组,并且将拒绝JSON,而JSON的第一项中确实包含4个键
请重述您的问题以使其更清楚吗?我根据假设做了一些解决方案,但是如果您可以确认我的理解,那就太好了。
请先阅读以下内容:
http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4.1
如果“ items”是模式数组,则每个元素的验证都将成功 实例的实例针对同一位置的架构进行验证,如果 任何。
加上上述主题的https://stackoverflow.com/a/52758108/2811843
https://json-schema.org/understanding-json-schema/reference/array.html#length
https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation
和https://json-schema.org/understanding-json-schema/reference/array.html一般
以及
https://json-schema.org/understanding-json-schema/reference/object.html#property-names
https://json-schema.org/understanding-json-schema/reference/object.html#size
和https://json-schema.org/understanding-json-schema/reference/object.html通常。
查看示例架构后,我将重新陈述问题陈述,以使您想得到一个需要架构的疯狂假设,该架构允许项目数组,其中item = object。第一项可能有4个键,而其他所有项都必须有5个键。
我需要一个可描述对象数组的JSON模式,其中 第一个对象始终具有4个键/属性,而其余所有对象 确实有5个键/属性。
另外,数组中总是至少有第一项(包含4个键),最多可以有X个 数组中的对象(包含5个键)。
进行元组输入和对象数组。因此,您可以准确地检查第一个项目(对象)是否恰好具有4个属性,并为其余所有属性定义架构。
首先,完整的工作模式(内部带有注释)。 “示例”部分包含用于说明逻辑的数组示例,只有最后3个对模式有效。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"$comment" : "This is an array, where first item must be an object with at least 4 properties and one property named \"state\" and can contain minimum 1 and maximum of 3 items",
"minItems" : 1,
"maxItems" : 3,
"items": [
{
"type": "object",
"minProperties" : 4,
"required" : ["state"],
}
],
"additionalItems" : {
"$comment" : "Any additional item in this array must be an object with at least 5 keys and two of them must be \"state\" and \"zip\".",
"type" : "object",
"minProperties" : 5,
"required" : ["state", "zip"],
},
"examples" : [
[
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
},
{},
{}
],
[
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "12345"
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
}
],
[
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "12345"
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "54321"
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "54321"
}
],
[],
[
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "12345"
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "54321"
},
],
[
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
},
],
[
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
},
{
"key1" : "1",
"key2" : "2",
"key3" : "3",
"state" : "some state",
"zip" : "12345"
},
]
]
}
所以,请逐步:
"type": "array",
"minItems" : 1,
"maxItems" : 3,
一个JSON的数组是最少1个项目,最多3个项目就可以了。如果您未定义“ minItems” 值,则空数组将通过针对架构的验证。
"items": [
{
"type": "object",
"minProperties" : 4,
"required" : ["state"],
}
],
这是元组魔术-元素的有限有序列表(序列)。是的,数学是这么说的。通过使用“ items”: [...] ,而不是 {...} < / strong>属于JSON模式验证规范(http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4.1)的上述部分。
上面基本上说:这是一个数组,其中第一项必须是具有至少4个键的对象,而这些键之一必须是“状态”。
好,最后但并非最不重要:
"additionalItems" : {
"$comment" : "Any additional item in this array must be an object with at least 5 keys and two of them must be \"state\" and \"zip\".",
"type" : "object",
"minProperties" : 5,
"required" : ["state", "zip"],
}
我这样说: 在此数组中(该数组必须第一项是具有4个键的对象,而其中一个键是“状态”,哦,顺便说一句,数组必须至少包含1个项,并且顶部必须包含3个项),您可以具有其他在“项目”部分中已定义的项目之上的项目。每个此类附加项必须是一个至少具有5个键的对象,其中两个键必须是“ state”和“ zip”。
它能解决您的问题吗?