jsonjsonschemaschema-design

How to reference json schema definition from another schema


I have a json schema representing a geometry as a Point or a MultiPoint. Each is defined within the schema in "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"
            }
        }
    }
}

Now I want to make another schema, which makes use of the Point definition. Currently, I copypasted the definitions for Point and position in the properties "startPosition" and "endPosition" and it works. But is there a way how to just reference the Point's definition from my geometry.json schema?

note: I only want to allow Point to be used here but not a MultiPoint - A geometry.json ref would allow both instead.

{
    "$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"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Solution

  • Haven't tested it myself, but according to this, you can make use of JSON pointers:

    In file geometry.json:

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "http://schema.my-site.org/geometry.json",
        "type": "object",
        "definitions": {
            "Point": { ...},
            "MultiPoint": {...}
        }
    }
    

    In file 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"
             }
        }
    }