【问题标题】:How do I do a nested list (array) of schema references in json schema如何在 json 模式中创建模式引用的嵌套列表(数组)
【发布时间】:2016-02-22 04:26:07
【问题描述】:

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

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

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

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

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

    {
        "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 模式的文档,看起来我的枚举定义对于字符串是正确的。
    • 请在新问题中添加您的示例和架构,然后查看。
    • 我想我发现了发生的事情并且似乎正在工作,我的枚举列表中有一对额外的 {},所以不是 { "enum": ["a","b"] } 我有 {"enum": { ["a","b"] } }。谢谢。
    • 我有一个类似的帖子在这里有类似的问题:stackoverflow.com/questions/35876240/…
    猜你喜欢
    • 1970-01-01
    • 2022-10-04
    • 2022-01-16
    • 2019-05-25
    • 2019-07-27
    • 2021-06-15
    • 1970-01-01
    • 2019-11-26
    • 2019-01-26
    相关资源
    最近更新 更多