如何从同一文件中的模式继承?

时间:2014-02-06 16:35:36

标签: json jsonschema

给出以下架构片段

{
    "$schema": "http://json-schema.org/schema#",
    "definitions": {
        "uuid": {
            "title": "uuid",
            "description": "UUID ( http://regex101.com/r/eJ7gN2 )",
            "type": "string",
            "minLength": 36,
            "maxLength": 36,
            "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
        },
        "reference": {
            "title": "reference",
            "description": "A reference to an object by UUID",
            "type": "object",
            "properties" : {
                "id": {
                    "$ref": "#/definitions/uuid"
                }
            },
            "required": [ "id" ]
        },
        "imageref": {
            "title": "imageref",
            "description": "Reference to an image using an internal UUID",
            "type": "object",
            "properties": {
                "size": {
                    "description": "Largest side of the image, depends on aspect ratio",
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 1600
                },
                "crop": {
                    "description": "Flag to tell the system to crop the image or not",
                    "type": "boolean",
                    "default": false
                }
            }
        }
    }
}

如何指定imageref应继承reference,以便imageref具有id中的reference属性?

我还有很多其他的reference类型的东西,我试图一遍又一遍地消除重复这个定义。

1 个答案:

答案 0 :(得分:3)

要从其他架构继承,请使用allOf并结合$ref

{
    "title": "Extension schema",
    "allOf": [{"$ref": "/path/to/base/schema"}]
}

继承同一文件中的另一个模式是一样的 - 您使用片段引用它,就像您在其他地方一样:

{
    "title": "Extension schema",
    "allOf": [{"$ref": "#/definitions/reference"}]
}