如何解决具有强制属性的不同类型数组的 JSON 模式验证失败
我创建了以下 Draft4 架构:
import matplotlib.pyplot as plt
for grp in list_groups:
plt.figure()
plt.plot(grp)
plt.show()
以下根据架构正确验证
{
"$schema": "http://json-schema.org/draft-04/schema#","$id": "catalog.structs.schema.json","definitions": {
"geography": {
"type": "object","description": "Geographical location which may or may not be part of a hierarchy.","properties": {
"geography_type": {
"type": "string","enum": ["union","country","state"]
},"name": {
"type": "string","enum": ["Turkey","Netherlands"]
},"parent": {
"type": "object","$ref": "#/definitions/geography"
}
},"required": ["name"]
},"geography_group": {
"type": "array","description": "A grouping of discrete geographies that are not necessarily in a common hierarchy.","items": {
"type" : "object","$ref": "#/definitions/geography"
}
}
},"oneOf": [{
"properties": {
"geography": { "type": "object","$ref": "#/definitions/geography"},"geography_group" : { "type": "object","$ref": "#/definitions/geography_group"}
},"additionalProperties": false
}]
}
但是,以下内容未能说明缺少所需的属性 name。
{"geography":{"name":"Turkey","geography_type":"country"}}
如果我删除 {"geography_group":[{"geography":{"name":"Turkey","geography_type":"country"}}]}
它可以工作,但我不想删除它。
我做错了什么?
解决方法
这里有一些误解,但大体上您的想法是正确的。
我不确定您认为 oneOf
在这里做什么。在这种情况下,它根本不是必需的,您可以将 properties
和 additionalProperties
直接放在根架构中。
就您的问题而言,虽然您已将 geography
定义为根级别的属性,但您还没有为 geography_group
值数组中的项目对象这样做。
您想将 geograph_group
定义更改为以下内容...
"geography_group": {
"type": "array","description": "A grouping of discrete geographies that are not necessarily in a common hierarchy.","items": {
"properties": {
"geograph": {
"$ref": "#/definitions/geography"
}
}
}
}
顺便说一句,在 JSON Schema 的草案 04 中,$ref
旁边的任何关键字都将被忽略。
您在 definitions
对象中使用的密钥与数据验证无关。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。