如何从另一个schema引用json schema定义

8

我有一个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"
            }
        }
    }
}

现在我想创建另一个模式,它利用了Point的定义。目前,我将Point和position的定义复制粘贴到属性“startPosition”和“endPosition”中,这样就可以使用了。但是,有没有一种方法可以仅从我的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"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
1个回答

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": {...}
    }
}

在文件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"
         }
    }
}

1
根据官方文档的说明,我们不应该相信验证器能正确处理任意URI。 - Raphael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接