[英]How to create JSON schema for dynamic nested map of objects
我正在尝试为以下 JSON 创建 JSON 架构,
{
"messages":{
"bookCreated":{
"default":{
"channels":{
"sqs":{
"enabled":true,
"topic":"sample-topic",
"shares":true
}
}
}
},
"bookCreationInProgress":{
"default":{
"channels":{
"sns":{
"enabled":true,
"topic":"sample-sns",
"shares":true
}
}
}
},
"bookCreationCompleted":{
"default":{
"channels":{
"s3":{
"enabled":true,
"topic":"sample-s3-bucket",
"shares":true
}
}
}
}
}
}
在消息bookCreated
、 bookCreationInProgress
、 bookCreationCompleted
中,我们同样有几个动态属性。 在这些对象中的每一个中,默认和通道详细信息都是强制性的。 每个通道都有一组强制属性。
我浏览了 inte.net 为上面的 json 创建了 JSON 模式,但是我无法获得关于如何为嵌套的 map 对象创建 json 模式的任何参考。
由于我无法为第一个动态 object 构建 json 模式,因此我无法进一步构建该模式。
{
"$schema": "app_messages",
"type": "object",
"additionalProperties": true,
"anyOf": [
{
"required": ["messages"]
}
],
"properties": {
"id": {
"type": "string"
}
}
}
如果有人能帮助我分享如何处理 JSON 模式中动态属性的 map 的指针,那就太好了。 任何帮助都会非常可观。
一个好的解决方案是使用“additionalProperties”作为 json 对象(而不是布尔值)组合到“$ref”和“$defs”。 一个会说话的例子可能是:
{
"$schema": "app_messages",
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#/$defs/subobj1" },
{ "$ref": "#/$defs/subobj2" }
]
},
"properties": {
"id": { "type": "string" }
},
"$defs": {
"subobj1": {
"type": "object",
"properties": {
"message": { "type": "string" }
...
},
"required": [ "message" ]
},
"subobj2": {
"type": "object",
"properties": {
"message": { "type": "string" }
...
},
"required": [ "message" ]
}
}
}
通过这种方式,main object 可以具有“id”属性,并且所有附加属性必须匹配子定义“subobj1”或“subobj2”之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.