【发布时间】:2021-04-19 04:51:14
【问题描述】:
我将编写 json 模式来验证树数据。 架构由顶部根和下面的块组成。 该块下方可能还有另一个块。
用于验证的架构。
schema = {
"$schema": "http://json-schema.org/draft-04/schema",
"$ref": "#/definitions/root",
"definitions":{
"root": {
"properties": {
"name": {
"type": "string"
},
"children": {
"type": "array",
"items": [
{"$ref":"#/definitions/block"}
]
}
},
"required": ["name", "children"]
},
"block": {
"properties": {
"name": {
"type": "string"
},
"children": {
"type": "array",
"items": [
{"$ref":"#/definitions/block"}
]
}
},
"required": ["name"]
}
}
}
以下是用于测试的错误数据。姓氏属性不存在。
{
"name": "group8",
"children": [
{
"name": "group7",
"children": [
{
"name": "group6",
"children": [
{
"name": "group5",
"children": [
{ ###### wrong
"children": []
}
]
}
]
}
]
}
]
}
此数据验证良好,但不适用于稍微复杂的树。
# Error: ValidationError: file /home/gulliver/.local/lib/python2.7/site-packages/jsonschema/validators.py line 934: 'name' is a required property #
{
"name": "group8",
"children": [
{
"name": "group7",
"children": [
{
"name": "group6",
"children": [
{
"name": "group12",
"children": [
{
"name": "group11",
"children": [
{
"name": "group10",
"children": []
}
]
}
]
},
{
"name": "group9",
"children": [
{
"name": "group5",
"children": [
{ ####### wrong
"children": []
}
]
}
]
}
]
}
]
},
{
"name": "group13",
"children": [
{
"name": "null1",
"children": []
}
]
}
]
}
树底数据无效时不起作用。 我的猜测是分支分裂并发生这种情况,有谁知道为什么或如何解决它?
我使用 python 和 jsonschema 进行了测试。
【问题讨论】:
-
在 draft-07 之前,我会避免在架构的根目录中使用 $ref。在某些实现中,这会导致错误。
标签: json jsonschema