繁体   English   中英

"json 模式来验证具有 anyOf 和 oneOf 要求的对象数组"

[英]json schema to validate array of objects with anyOf and oneOf requirements

我正在尝试定义一个 json 模式来限制数组中包含的对象的属性。

到目前为止,我所拥有的是:

{
    "title":"myCollection",
    "properties":{
        "things":{
            "type":"array",
            "items":[{
                "title":"thingObj",
                "type":"object",
                "properties":{
                    "name":{
                        "type":"string"
                    },
                    "code":{
                        "type":"string"
                    },
                    "type":{
                         "type":"string",
                         "enum":["dog","cat"]
                    },
                    "rate":{
                        "type":"number"
                    },
                    "value":{
                        "type":"number"
                    }
                },
                "anyOf":[{
                    "properties":{
                        "name":{
                            "type":"string"
                        }
                    },"required":["name"]
                },{
                    "properties":{
                        "code":{
                            "type":"string"
                        }
                    },"required":["code"]
                },{
                    "properties":{
                        "type":{
                            "type":"string",
                            "enum":["new","existing"]
                        }
                    },"required":["type"]
                }],
                "oneOf":[{
                    "properties":{
                        "rate":{
                            "type":"number"
                        }
                    },
                    "required":["rate"]
                },{
                   "properties":{
                       "value":{
                            "type":"number"
                       }
                   },
                   "required":["value"]
                }],
                "additionalProperties":false
            }]
        }
    }
}

这是因为你(不小心)使用了“元组输入”。 "items"的值是一个数组时,它会启用,并且它将模式与数组中的特定位置匹配。

如果将( "items" (在模式中)更改为模式(不是模式数组),那么它将以相同的方式验证所有项目。

感谢@cloudfeet 的回答,在看到他的回答之前,我一直在努力解决这个问题。 为了更清楚,应该删除项目周围的 []。

{
    "title":"myCollection",
    "properties":{
        "things":{
            "type":"array",
            "items":**[**{
                "title":"thingObj",
                "type":"object",
                .
                .
                .
                   "required":["value"]
                }**]**,
                "additionalProperties":false
            }]
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM