如何从另一个模式

时间:2017-01-24 11:22:35

标签: json jsonschema schema-design

我有一个json模式,将几何体表示为Point或MultiPoint。每个都在"定义":

中的模式中定义
{
    "$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"中复制了Point和position的定义。和" endPosition"它的工作原理。但是有没有办法如何从我的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"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我自己没有测试过,但根据this,您可以使用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"
         }
    }
}