json - 数组中严格对象的 JSON Schema
问题描述
我创建了一个 JSON 模式来验证一个简单的 JSON 文件。好消息是它以我想要的方式进行验证,因为任何数量的预订元素可以以任何顺序出现,并且在每种类型的预订元素中都不允许有额外的属性。
理想情况下,我想删除 JSON 模式中的 bookingElement 对象(id、type、depair、destair、city)中可能属性的完整列表,只留下 oneOf 列表,这些列表清楚地显示了每种不同类型中允许哪些字段的元素。
任何人都可以提供没有仍然适用严格规则的完整列表的架构版本吗?
这是 JSON:
{
"bookingElements": [
{
"id" : "00003",
"type" : "flight",
"depair" : "LHR",
"destair" : "CDG"
},
{
"id" : "00008",
"type" : "hotel",
"city" : "Paris"
}
]
}
架构是:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"bookingElements": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"depair": {
"type": "string"
},
"destair": {
"type": "string"
},
"city": {
"type": "string"
}
},
"oneOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"depair": {
"type": "string"
},
"destair": {
"type": "string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
]
}
}
},
"required": [
"bookingElements"
]
}
理想情况下,JSON 模式看起来更接近于以下内容:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"bookingElements": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"oneOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"depair": {
"type": "string"
},
"destair": {
"type": "string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
]
}
}
},
"required": [
"bookingElements"
]
}
解决方案
推荐阅读
- django - AttributeError:“ManyToManyDescriptor”对象没有“删除”属性
- typescript - 子对象中键的打字稿对象映射
- swift - 如何从 FileDocument 中获取文件名
- sas - SAS:如何组合文本和日期字段
- reactjs - 如何在 VictoryLegend 组件的 style 道具内圆角?
- php - 具有默认参数值的 php7 函数存在问题
- python-3.x - 写入文件时出现 Apache Spark o27.csv 错误
- html - 需要帮助将页脚的内容居中
- python - 阻塞 Tyk Gateway 中间件导致请求排队
- web-manifest - android“安装应用程序”导致“添加到主屏幕”具有不同的图标