首先要做的事情:您想通过以下架构定义实现什么目标?
"first" : [ { ...schema... } ]
至于您的问题陈述,我不确定您想要实现什么:
允许第一个数组项是具有 4 个键的对象,而所有其他项应具有 5 个键的架构?
架构,只允许数组 items=object 有 5 个键,并且会拒绝 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 一般。
可能的解决方案
在查看示例架构后,我将重新表述问题陈述,做出一些疯狂的假设,您需要一个架构,它允许一个项目数组,其中项目 = 对象。第一项可以有 4 个键,而所有其他项必须有 5 个键。
我需要一个 JSON 模式来描述一组对象,其中
第一个对象总是有 4 个键/属性,而所有剩余的对象
确实有 5 个键/属性。
此外,数组中总是至少有第一项(包含 4 个键),最多可以有 X 个其他
数组中的对象(包含 5 个键)。
使用元组类型和对象数组。因此,您可以准确地检查第一项(对象)是否正好有 4 个属性,并为其余的属性定义架构。
首先,完整的工作模式(内部包含 cmets)。 “示例”部分包含用于说明逻辑的数组示例,只有最后 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" : [ ... ] 而不是 { ... } 您属于 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”。
它能解决你的问题吗?