3

我正在尝试构建一个架构,其中包含我想要强制架构的项目列表。

基本上这里是我要针对架构验证的数据:

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"]
}
4

1 回答 1

3

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

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

但是您的对象具有以下形式:

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

实现此目的的一种方法是additionalPropertiesitems子句中使用:

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

如果您可以对这些属性键强制执行一些规则(例如,所有都必须以badge结尾),那么您可以使用patternProperties正则表达式的子句。

于 2016-02-22T11:48:13.797 回答