基于属性之一的JSON模式anyOf验证 [英] JSON schema anyOf validation based on one of properties
本文介绍了基于属性之一的JSON模式anyOf验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难根据属性之一的值来弄清楚如何验证对象数组.所以我有一个JSON对象,如:
I'm having difficulty figuring out how to validate an array of objects based on the value of one of the properties. So where I have a JSON object like:
{
"items": [
{
"name": "foo",
"otherProperty": "bar"
},
{
"name": "foo2",
"otherProperty2": "baz",
"otherProperty3": "baz2"
},
{
"name": "imInvalid"
}
]
}
我想说
- 项目可以包含anyOf对象,其中名称可以是"foo"或"foo2"
- 如果它是"foo",则唯一有效的其他属性(必需)是 "otherProperty"
- 如果名称是"foo2",则唯一有效的其他 属性分别是"otherProperty2"和"otherProperty3"
- 除"foo"和"foo2"外,"name"的其他任何值均无效
- 对象本身在items数组中是可选的,有些对象可能会重复.
- items can contain anyOf objects where name can be "foo" or "foo2"
- if it's "foo" then the only valid other property (required) is "otherProperty"
- if the name is "foo2" then the only valid other properties are "otherProperty2" and "otherProperty3" both required
- No other value for "name" than "foo" and "foo2" is valid
- The objects themselves are optional in the items array, and some might be repeated.
我已经尝试过各种方法,但是当我通过验证时似乎并没有失败.例如,名称"imInvalid"应引起验证错误.这是我对架构的最新迭代.我想念什么?
I've tried all kinds of things but I can't seem to get a failure when I validate. For example, the name "imInvalid" should be causing a validation error. This is my latest iteration of the schema. What am I missing?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"additionalProperties": false,
"properties": {
"name": {
"anyOf": [
{
"type": "object",
"required": ["name", "otherProperty"],
"additionalProperties": false,
"properties": {
"otherProperty": { "type": "string" },
"name": { "enum": [ "foo" ] }
}
},{
"type": "object",
"required": ["name", "otherProperty2", "otherProperty3" ],
"additionalProperties": false,
"properties": {
"otherProperty2": { "type": "string" },
"otherProperty3": { "type": "string" },
"name": { "enum": [ "foo2" ] }
}
}
]
}
}
}
}
}
推荐答案
您已经有了使用枚举来分隔匹配项的基本思想,但是这里有一些错误:
You've got the basic idea of using enum to separate what's matching, but there's a couple of mistakes here:
- Json模式数组没有属性,它们具有项目.
- 在这些属性中,您将name定义为一个属性,该属性随后包含其他json模式对象.
- 在架构的第二个分支中,您定义了
otherProperty3
,但在示例中,该属性称为anotherProperty3
- Json schema arrays don't have properties, they have items.
- Within that properties you're defining name as an attribute that then holds other json schema objects.
- In the second branch of you're schema you defined
otherProperty3
but in your sample that property is calledanotherProperty3
尝试这个经过稍微修改的版本:
Try this slightly modified version:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"additionalProperties": false,
"items": {
"anyOf": [
{
"type": "object",
"required": ["name", "otherProperty"],
"additionalProperties": false,
"properties": {
"otherProperty": { "type": "string" },
"name": { "enum": [ "foo" ] }
}
},{
"type": "object",
"required": ["name", "otherProperty2", "anotherProperty3" ],
"additionalProperties": false,
"properties": {
"otherProperty2": { "type": "string" },
"anotherProperty3": { "type": "string" },
"name": { "enum": [ "foo2" ] }
}
}
]
}
}
}
}
这篇关于基于属性之一的JSON模式anyOf验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文