本文介绍了如何在JSON模式中嵌套模式引用的嵌套列表(数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个架构,该架构包含我想要具有强制架构的项目列表.

I am trying to build a schema which has a list of items that I want to have an enforced schema.

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

Basically here is the data I want to validate against the schema:

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
            }


        }
    ],
}

我的模式映射本身就是这样,但是在尝试验证时似乎出现错误:

And my schema mapping as is such but seem to be getting errors when trying to validate:

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

我基本上希望检查清单中包含子清单,但也要根据该类型的物品进行验证.

I basically want sub list within the inspection list but also to validate based on that type of item.

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

SOLUTION:Thanks to jruizaranguren's help the solution was:

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

推荐答案

您遇到的问题是,您正在将数组中的每个项目约束为以下形式:

The problem you have is that you are constraining each item in the array to be of the form:

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

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

But your objects are of the form:

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

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

One way to achieve this would be to use additionalProperties in the items clause:

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

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

If you can enforce some rules on these properties keys (for instance, all must end with badge) then you may use patternProperties clause with a regular expression.

这篇关于如何在JSON模式中嵌套模式引用的嵌套列表(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 22:46
查看更多