如何在Json Schema中创建一个嵌套的模式引用列表(数组)

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"]
}
1个回答

3
您面临的问题是您将数组中的每个项限制为以下形式:
{
    "expected": "yes",
    "image": "/image/path/here",
    "state": 1
}

但是你的对象形式为:
{ "door_badge" : 
    {
        "expected": "yes",
        "image": "/image/path/here",
         "state": 1
    },
}

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

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

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


实际上还有一个跟进的问题,似乎有些奇怪。我试图设置{"expected": {"enum": ["yes", "maybe"]}},但是出现了验证错误,但是枚举似乎适用于具有数字而不是字符串的状态。我参考了JSON模式的文档,看起来我的枚举定义对于字符串是正确的。 - xamox
1
请在新问题中添加您的示例和模式,我们会查看。 - jruizaranguren
我想我发现了问题所在,看起来已经解决了。我的枚举列表中多了一对 {},所以应该是 { "enum": ["a","b"] } 而不是 { "enum": { ["a","b"] } }。谢谢。 - xamox
我在这里有一个类似的帖子,涉及到相似的问题:https://dev59.com/C90_04gBFxS5KdRjL2DC - xamox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接