如何从另一个模式引用 json 模式定义
How to reference json schema definition from another schema
我有一个 json 模式,将几何图形表示为点或多点。每个都在 "definitions":
中的模式中定义
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/geometry.json#",
"type": "object",
"oneOf": [
{
"allOf": [
{
"required": [
"type",
"coordinates"
]
},
{
"oneOf": [
{
"$ref": "#/definitions/Point"
},
{
"$ref": "#/definitions/MultiPoint"
}
]
}
]
}
],
"definitions": {
"Point": {
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"$ref": "#/definitions/position"
}
}
},
"MultiPoint": {
"title": "MultiPoint",
"type": "object",
"properties": {
"type": {
"enum": [
"MultiPoint"
]
},
"coordinates": {
"$ref": "#/definitions/positionArray"
}
}
},
"position": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
},
"positionArray": {
"type": "array",
"items": {
"$ref": "#/definitions/position"
}
}
}
}
现在我想制作另一个模式,它使用了 Point 定义。目前,我在属性 "startPosition" 和 "endPosition" 中复制粘贴了 Point 和 position 的定义并且它有效。但是有没有办法从我的 geometry.json 模式中引用 Point 的定义?
注意:我只想允许在这里使用 Point 而不是 MultiPoint - geometry.json ref 将允许两者。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/myitem.json#",
"type": "object",
"additionalProperties": false,
"required": [
"myproperty"
],
"properties": {
"myproperty": {
"type": "array",
"minItems": 0,
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"startPosition": {
"geometry": {
"required": [
"type",
"coordinates"
],
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
}
},
"endPosition": {
"geometry": {
"required": [
"type",
"coordinates"
],
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
}
}
}
}
}
}
}
自己没测试过,但是根据this, you can make use of JSON pointers:
在文件 geometry.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/geometry.json",
"type": "object",
"definitions": {
"Point": { ...},
"MultiPoint": {...}
}
}
在文件 myitem.json 中:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/myitem.json#",
"type": "object",
"properties": {
"point": {
"$ref": "http://schema.my-site.org/geometry.json#definitions/Point"
}
}
}
我有一个 json 模式,将几何图形表示为点或多点。每个都在 "definitions":
中的模式中定义{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/geometry.json#",
"type": "object",
"oneOf": [
{
"allOf": [
{
"required": [
"type",
"coordinates"
]
},
{
"oneOf": [
{
"$ref": "#/definitions/Point"
},
{
"$ref": "#/definitions/MultiPoint"
}
]
}
]
}
],
"definitions": {
"Point": {
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"$ref": "#/definitions/position"
}
}
},
"MultiPoint": {
"title": "MultiPoint",
"type": "object",
"properties": {
"type": {
"enum": [
"MultiPoint"
]
},
"coordinates": {
"$ref": "#/definitions/positionArray"
}
}
},
"position": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
},
"positionArray": {
"type": "array",
"items": {
"$ref": "#/definitions/position"
}
}
}
}
现在我想制作另一个模式,它使用了 Point 定义。目前,我在属性 "startPosition" 和 "endPosition" 中复制粘贴了 Point 和 position 的定义并且它有效。但是有没有办法从我的 geometry.json 模式中引用 Point 的定义?
注意:我只想允许在这里使用 Point 而不是 MultiPoint - geometry.json ref 将允许两者。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/myitem.json#",
"type": "object",
"additionalProperties": false,
"required": [
"myproperty"
],
"properties": {
"myproperty": {
"type": "array",
"minItems": 0,
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"startPosition": {
"geometry": {
"required": [
"type",
"coordinates"
],
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
}
},
"endPosition": {
"geometry": {
"required": [
"type",
"coordinates"
],
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
}
}
}
}
}
}
}
自己没测试过,但是根据this, you can make use of JSON pointers:
在文件 geometry.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/geometry.json",
"type": "object",
"definitions": {
"Point": { ...},
"MultiPoint": {...}
}
}
在文件 myitem.json 中:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/myitem.json#",
"type": "object",
"properties": {
"point": {
"$ref": "http://schema.my-site.org/geometry.json#definitions/Point"
}
}
}