构建一个复杂的模式

Reuse

这部分内容是草案4新增内容,而草案3没有。

你可以使用关键字definitions预先定义部分schema。

// a schema
{
    "type": "object",
    "properties": {
        "street_address": { "type": "string" },
        "city": { "type": "string" },
        "state": { "type": "string" }
    },
    "required": ["street_address", "city", "state"]
}

也可以写成:

// a schema
{
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "street_address": { "type": "string" },
                "city": { "type": "string" },
                "state": { "type": "string" }
            },
            "required": ["street_address", "city", "state"]
        }
    }
}

当使用时,我们用$ref关键字进行引用,$ref值时一个JSON Pointer

{ "$ref": "#/definitions/address" }

如下示例:

// a schema
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "street_address": { "type": "string" },
                "city": { "type": "string" },
                "state": { "type": "string" }
            },
            "required": ["street_address", "city", "state"]
        }
    },
    "type": "object",
    "properties": {
        "billing_address": { "$ref": "#/definitions/address" },
        "shipping_address": { "$ref": "#/definitions/address" }
    }
}
// success
{
    "shipping_address": {
        "street_address": "1600 Pennsylvania Avenue NW",
        "city": "Washington",
        "state": "DC"
    },
    "billing_address": {
        "street_address": "1st Street SE",
        "city": "Washington",
        "state": "DC"
    }
}

The id property

id关键字有两个用途:

  • 被声明用来唯一标识一个schema。
  • 用来作为$ref的base url被解析。

例如,如果有以下声明:

{ "id": "http://foo.bar/schemas/address.json" }

那么:

{ "$ref": "person.json" }

此时,person.json的url为http://foo.bar/schemas/person.json

Extending

$ref的强大之处在于它同组合模式一起使用。

// a schema
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "street_address": { "type": "string" },
                "city": { "type": "string" },
                "state": { "type": "string" }
            },
            "required": ["street_address", "city", "state"]
        }
    },
    "type": "object",
    "properties": {
        "billing_address": { "$ref": "#/definitions/address" },
        "shipping_address": {
            "allOf": [{
                "$ref": "#/definitions/address"
            }, {
                "properties": { "type": { "enum": [ "residential", "business" ] } },
                "required": ["type"]
            }]
        }
    }
}
// failure
{
    "shipping_address": {
        "street_address": "1600 Pennsylvania Avenue NW",
        "city": "Washington",
        "state": "DC"
    }
}
// success
{
    "shipping_address": {
        "street_address": "1600 Pennsylvania Avenue NW",
        "city": "Washington",
        "state": "DC",
        "type": "business"
    }
}

results matching ""

    No results matching ""