我正在设计一个带有递归元素的 JSON 模式。该元素不在根,而是在子模式中:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "http://example.org/json-schema/struct.schema.json",
"type": "array",
"items": {
"title": "Structure",
"type": "object",
"properties": {
"id": {
"title": "ID",
"type": "string"
},
"label": {
"title": "Label",
"type": "string"
},
"children": {
"$anchor": "children",
"title": "Children",
"type": "array",
"items": {
"title": "Structure Item",
"type": "object",
"properties": {
"id": {
"title": "ID",
"type": "string"
},
"href": {
"title": "Reference",
"type": "string"
},
"label": {
"title": "Structure Item Label",
"type": "string"
},
"sort_label": {
"title": "Sort Label",
"type": "string"
},
"children": {"$ref": "children"}
}
},
"anyOf": [
{"required": ["id", "href"]},
{"required": ["id", "children"]}
]
}
},
"required": ["id", "label", "children"]
}
}
当我尝试编译模式时,python-fastjsonschema
由于items.properties.children
.
从https://json-schema.org/understanding-json-schema/structuring#recursion中的示例中,我不清楚什么是允许递归的,什么是不允许的。不允许$ref
在 s 内递归s,但s 到根锚点会在有效示例中列出。它们都创建了一个循环(这确实是递归的目的!)。我认为对于非根锚点来说是可以的,但显然不是。$def
$ref
$ref
我还尝试使用未命名的锚点,如"children": {"$ref": "#/items/properties/children"}
,并且模式编译,但在验证时抛出错误。
我该如何建模这个模式?