如何在json模式中执行模式引用的嵌套列表(数组)

时间:2016-02-22 04:26:08

标签: json jsonschema json-schema-validator

我正在尝试构建一个架构,其中包含我希望拥有强制架构的项目列表。

基本上这里是我想要根据模式验证的数据:

data = {
    "VIN" : "123",
    "timestamp" : "xxxx",
    "model" : "jeep",
    "inspections": [
        { "door_badge" : 
             {
                "expected": "yes",
                "image": "/image/path/here",
                "state": 1
            },
        },
        {
            "rear_badge" :
            {
                "expected" : "yes",
                "image" : "/image/path/here",
                "state": 1
            }


        }
    ],
}

我的架构映射就是这样,但在尝试验证时似乎遇到了错误:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type": "string" },
                "found": { "type" : "string"},
                "state" : { "enum" : [0,1] },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image"]
        },
        "inspections" : {
            "type" : "array",
            "items" : {
                "$ref" : "#/definitions/inspection"
            },
            "required" : ["items"]
        },

    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { "$ref" : "#/definitions/inspections"}
    },
    "required": ["VIN", "timestamp", "model"]
}

我基本上想要检查清单中的子清单,但也要根据该类型的物品进行验证。

解: 感谢jruizaranguren的帮助,解决方案是:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type": "string" },
                "found": { "type" : "string"},
                "state" : { "enum" : [0,1] },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image", "expected"]
        },
    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }
    },
    "required": ["VIN", "timestamp", "model", "inspections"]
}

1 个答案:

答案 0 :(得分:2)

您遇到的问题是您将数组中的每个项目限制为以下形式:

{
    "expected": "yes",
    "image": "/image/path/here",
    "state": 1
}

但你的对象的形式是:

{ "door_badge" : 
    {
        "expected": "yes",
        "image": "/image/path/here",
         "state": 1
    },
}

实现此目的的一种方法是在additionalProperties子句中使用items

"items" : 
    { 
            "type" : "object",
            "maxProperties": 1,
            "minProperties": 1,
            "additionalProperties" : {
                "$ref" : "#/definitions/inspection"
            }
    }

如果您可以对这些属性键执行某些规则(例如,所有必须以徽章结尾),那么您可以将patternProperties子句与正则表达式一起使用。