我有一个JSON架构
{ 'description': 'TPNode', 'type': 'object', 'id': 'tp_node', 'properties': { 'selector': { 'type': 'string', 'required': true }, 'attributes': { 'type': 'array', 'items': { 'name': 'string', 'value': 'string' } }, 'children': { 'type': 'array', 'items': { 'type': 'object', '$ref': '#' } }, 'events': { 'type': 'array', 'items': { 'type': 'object', 'properties': { 'type': { 'type': 'string' }, 'handler': { 'type': 'object' }, 'dependencies': { 'type': 'array', 'items': { 'type': 'string' } } } } } } }
我想在children属性中表达的是它是一个具有相同精确模式的对象数组.这是描述它的正确方法吗?
使用id
您需要引用的架构
'$ref': 'tp_node'
见这里:http: //json-schema.org/latest/json-schema-core.html#anchor30
使用定义和$ ref.
您可以将以下架构复制并粘贴到此在线json/schema编辑器并检查结果.
编辑截图:
架构代码:
{ "definitions": { "TPNode": { "title": "TPNode", "description": "TPNode", "type": "object", "properties": { "selector": { "type": "string", "required": true }, "attributes": { "type": "array", "items": { "title": "Attribute", "type": "object", "properties": { "name": { "type": "string" }, "value": { "type": "string" } } } }, "children": { "type": "array", "items": { "$ref": "#/definitions/TPNode" } }, "events": { "type": "array", "items": { "title": "Event", "type": "object", "properties": { "type": { "type": "string" }, "handler": { "type": "object" }, "dependencies": { "type": "array", "items": { "type": "string" } } } } } } } }, "$ref": "#/definitions/TPNode" }
是的,您的架构将起作用.该"$ref": "#"
点回模式文档的根.
但是,"type": "object"
没用:
{ 'type': 'object', '$ref': '#' }
如果$ref
存在,则忽略所有其他关键字.最好type
从#/properties/children/items
架构中删除.