我尝试为我的数据编写json模式。数据看起来就是这样:
{
"gold": [
{
"id": "goldOne",
"name": "firstGold",
"title": "Gold 1 earned"
},
{
"id": "goldTwo",
"name": "secondGold",
"title": "Gold 2 earned"
}
],
"silver": [
{
"id": "silberOne",
"name": "firstSilver",
"title": "Silver!"
}
],
"bronze": [
{
"id": "bronzeOne",
"name": "firstBronze",
"title": "Bronze!"
}
]
}
我已经为“黄金”阵列创建了架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title" : "trophy descriptions",
"type": "object",
"properties": {
gold: {
"description": "gold trophies",
"type":"array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "unique identifier"
},
"name": {
"type": "string",
"description": "label of trophy"
},
"title": {
"type": "string",
"description": "description of trophy"
}
}
}
}
}
}
因为“银”和“青铜” - 阵列包含与“黄金”完全相同类型的元素,我想知道我是否必须写下相同的东西三次或者我是否可以参考单一描述?
答案 0 :(得分:3)
是的,您可以通过$ ref keyword:
定义和引用模式{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "trophy descriptions",
"type" : "object",
"properties" : {
"gold" : {
"$ref" : "#/definitions/medal"
},
"silver" : {
"$ref" : "#/definitions/medal"
},
"bronze" : {
"$ref" : "#/definitions/medal"
}
},
"definitions" : {
"medal" : {
"type" : "array"
// and whatever you want here
}
}
}