在下面的示例架构中,我基本上要要求reporting_person
inincident
对象是数组中person
引用的对象people
。这可能吗?
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"person": {
"type": "object",
"properties": {
"person_id": {
"type": "integer"
},
"first_name": {
"type": "string"
}
}
},
"incident": {
"type": "object",
"properties": {
"incident_id": {
"type": "integer"
},
"description": {
"type": "string"
},
"reporting_person": {
"type": "string",
"$ref": "#/people/person_id"
}
}}
},
"type": "object",
"properties": {
"root" :{
"type" : "object",
"properties": {
"people" : {
"type" :"array",
"items" : { "$ref": "#/definitions/person"}
},
"incidents" : {
"type" : "array",
"items": {"$ref": "#/definitions/incident"}
}
}
}
}
}
我的另一种选择是仅使用模式定义ID标准,然后将确认移至应用程序代码,但是如果不在模式中进行这种排序数据有效性确认,将是很可惜的。谢谢
你可以$ref
用来在任何地方引用任何模式。在这种情况下,你$ref
应为"#/definitions/person/properties/person_id
。但是,通常为“ person_id”创建定义并引用它而不是指向属性是更干净的方法。
我还注意到你"type": "string
旁边还有一个$ref
。这type
将被忽略。如果$ref
该对象中存在,则该对象将被视为引用,而不是架构。因此,“类型”不被评估为type
关键字。这只是参考中的噪音。