所以我有一个json模式,在这个模式中,我在同一个json文件中引用另外两个模式,这很好.
{
"id": "http://ourdns.co.za/public/assets/json/formSchema.json",
"type": "object",
"properties": {
"person": {
"type": "object",
"id": "#person",
"properties": {
"first_name": {
"title": "What is your first name",
"type": "string"
},
"last_name": {
"title": "What is your last name",
"type": "string"
}
}
},
"person_api": {
"type": "object",
"id": "#person"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是一个根json模式,它引用了根外部的两个其他json模式.这与我当前的架构不同,我在一个文件中有所有架构(不理想).有一个小问题,我不能$ref用作引用关键字,因为我们使用的插件不支持这个.但是我们发现id可以用作引用关键字.(JsonForm是插件).然后我们如何使用id关键字获取这些,因为它似乎不起作用?
{
"id": "http://ourdns.co.za/public/assets/json/formSchema.json",
"type": "object",
"properties": {
"person_api": {
"type": "object",
"id": "public/assets/person.json"
}
}
}
Run Code Online (Sandbox Code Playgroud)
1)我们如何在外部调用相同的数据,比如... "id": "public/assets/person.json"而不是将它们全部合并到一个文件中?2)如果我们只需要person.firstname来自person.json架构,我们如何检索特定属性?
{
"id": "http://dsn.co.za/public/assets/json/person.json",
"type": "object",
"properties": {
"first_name": {
"title": "What is your first name",
"type": "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
您无法单独执行引用id.对于参考,您必须使用$ref.
该id关键字允许您提供架构的URL作为引用的目标:
{
"id": "http://example.com/schemas/example",
"type": "object",
"properties": {
"arr1": {
"id": "#item-schema",
...
},
"arr2": {"$ref": "#item-schema"}
}
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以使用漂亮的URL(例如http://example.com/schemas/example#item-schema)而不是使用JSON Pointer片段语法来引用模式.它还允许您重新组织架构(例如,将项目架构移动到其中definitions)而不更改URL.
但是,对于引用本身,您仍然需要使用$ref.如果您需要此功能,则需要在您使用的任何工具中支持它.