如何从另一个模式引用 json 模式定义

Moj*_*oMS 8 json schema-design jsonschema

我有一个 json 模式,将几何图形表示为点或多点。每个都是在“定义”中的模式中定义的:

{
    "$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"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想创建另一个模式,它使用点定义。目前,我在属性“startPosition”和“endPosition”中复制粘贴了 Point 和位置的定义,并且它有效。但是有没有办法从我的 Geometry.json 架构中引用 Point 的定义?

注意:我只想允许在此处使用 Point,但不允许使用 MultiPoint - Geometry.json 引用将允许两者都使用。

{
    "$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"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

bav*_*aza 9

我自己还没有测试过,但是根据这个,你可以使用JSON 指针

在文件 Geometry.json 中:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://schema.my-site.org/geometry.json",
    "type": "object",
    "definitions": {
        "Point": { ...},
        "MultiPoint": {...}
    }
}
Run Code Online (Sandbox Code Playgroud)

在文件 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"
         }
    }
}
Run Code Online (Sandbox Code Playgroud)