我有一个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'
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想在children属性中表达的是它是一个具有相同精确模式的对象数组.这是描述它的正确方法吗?
clo*_*eet 19
是的,您的架构将起作用.该"$ref": "#"点回模式文档的根.
但是,"type": "object"没用:
{
'type': 'object',
'$ref': '#'
}
Run Code Online (Sandbox Code Playgroud)
如果$ref存在,则忽略所有其他关键字.最好type从#/properties/children/items架构中删除.
Ser*_*min 17
使用id您需要引用的架构
'$ref': 'tp_node'
Run Code Online (Sandbox Code Playgroud)
见这里:http: //json-schema.org/latest/json-schema-core.html#anchor30
小智 9
递归示例。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"person": {
"type": "object",
"properties": {
"name": { "type": "string" },
"children": {
"type": "array",
"items": { "$ref": "#/definitions/person" },
"default": []
}
}
}
},
"type": "object",
"properties": {
"person": { "$ref": "#/definitions/person" }
}
}
Run Code Online (Sandbox Code Playgroud)
使用定义和$ 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"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14440 次 |
| 最近记录: |