Question

Given the following schema fragment

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

How do I specify that imageref should inherit from reference, so that imageref has the id property that is in reference?

I have lots of other things that will be reference types and I am trying to eliminate duplicating that definition over and over again.

Était-ce utile?

La solution

To inherit from another schema, you use allOf combined with $ref:

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

Inheriting from another schema within the same file is just the same - you reference it using fragments, just like you have elsewhere:

{
    "title": "Extension schema",
    "allOf": [{"$ref": "#/definitions/reference"}]
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top