Json模式中的“type”是可选的

时间:2015-03-24 10:36:59

标签: json jsonschema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product set",
    "type": "array",
    "items": {
        "title": "Product",
        "type": "object",
        "properties": {
            "id": {
                "description": "The unique identifier for a product",
                "type": "number"
            },
            "name": {
                "type": "string"
            },
            "price": {
                "type": "number",
                "minimum": 0,
                "exclusiveMinimum": true
            },
            "tags": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "minItems": 1,
                "uniqueItems": true
            },
            "dimensions": {
                "type": "object",
                "properties": {
                    "length": {"type": "number"},
                    "width": {"type": "number"},
                    "height": {"type": "number"}
                },
                "required": ["length", "width", "height"]
            },
            "warehouseLocation": {
                "description": "Coordinates of the warehouse with the product",
                "$ref": "http://json-schema.org/geo"
            }
        },
        "required": ["id", "name", "price"]
    }
}

在上面的Json架构"维度"是"类型":"对象" ,是类型可选的,如果"类型"如果我认为它是对象,则不指定。找不到与可选元素相关的规格中的任何内容。

2 个答案:

答案 0 :(得分:1)

http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1

If the property is not defined or is not in this list, then any
type of value is acceptable.

type是可选的,如果省略type,则可以接受任何值。

答案 1 :(得分:-1)

我认为是可选的。正如您在下面的元架构中所看到的,属性" type"没有默认值:

http://json-schema.org/schema

同样在我的JSON模式库NJsonSchema中,我将类型设置为None而不是Object。查看TypeRaw属性:

https://github.com/rsuter/NJsonSchema/blob/master/NJsonSchema/JsonSchema4.Serialization.cs

当默认值设置为Object时,JSON Schema测试套件中的更多测试失败。也许你可以在这个JSON Schema测试套件中找到一个明确的答案:

https://github.com/json-schema/JSON-Schema-Test-Suite