我们如何 link 一个 jsonschema 到另一个 jsonschema
how can we link one jsonschema to another jsonschema
文件的路径是 E:\JSONSchema\Files\details.json
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"tagid": {
"type": "string"
}
},
"required": [
"id",
"tagid"
],
"additionalProperties": false
}
}
我想在位于 E:\JSONSchema\Core\visuals.json
的另一个文件中重复使用上面的 jsonschema
。我怎样才能实现它?
JSON 架构允许使用具有单个 $ref
属性 的对象来引用外部定义的架构,无论何处需要架构对象。规范的相关部分是 here:
Any time a subschema is expected, a schema may instead use an object
containing a "$ref" property. The value of the $ref is a URI
Reference. Resolved against the current URI base, it identifies the
URI of a schema to use. All other properties in a "$ref" object MUST
be ignored.
所以按照你的例子,这应该作为 e:\JSONSchema\Core\visuals.json
:
的内容
{
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"IDs": {
"$ref": "../files/details.json"
}
}
}
使用 $ref
并提供绝对路径作为值
示例:
文件路径:E:\JSONSchema\Files\details.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"reuse": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"tagid": {
"type": "string"
}
},
"required": [
"id",
"tagid"
],
"additionalProperties": false
}
}
}
如果我想在另一个文件中重复使用,示例代码如下所示
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"IDs": {
"$ref": "file:/E:/JSONSchema/Files/details.json#/reuse"
}
}
}
实现此目的的另一种方法是使用 id
。
检查以下代码。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/E:/JSONSchema/Files/details.json",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"IDs": {
"$ref": "#/reuse"
}
}
}
文件的路径是 E:\JSONSchema\Files\details.json
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"tagid": {
"type": "string"
}
},
"required": [
"id",
"tagid"
],
"additionalProperties": false
}
}
我想在位于 E:\JSONSchema\Core\visuals.json
的另一个文件中重复使用上面的 jsonschema
。我怎样才能实现它?
JSON 架构允许使用具有单个 $ref
属性 的对象来引用外部定义的架构,无论何处需要架构对象。规范的相关部分是 here:
Any time a subschema is expected, a schema may instead use an object containing a "$ref" property. The value of the $ref is a URI Reference. Resolved against the current URI base, it identifies the URI of a schema to use. All other properties in a "$ref" object MUST be ignored.
所以按照你的例子,这应该作为 e:\JSONSchema\Core\visuals.json
:
{
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"IDs": {
"$ref": "../files/details.json"
}
}
}
使用 $ref
并提供绝对路径作为值
示例:
文件路径:E:\JSONSchema\Files\details.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"reuse": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"tagid": {
"type": "string"
}
},
"required": [
"id",
"tagid"
],
"additionalProperties": false
}
}
}
如果我想在另一个文件中重复使用,示例代码如下所示
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"IDs": {
"$ref": "file:/E:/JSONSchema/Files/details.json#/reuse"
}
}
}
实现此目的的另一种方法是使用 id
。
检查以下代码。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/E:/JSONSchema/Files/details.json",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"IDs": {
"$ref": "#/reuse"
}
}
}