我要问自己的是,我是否可以级联多个'oneOf‘,或者有更好的方法使我的案例有效。
我试图验证以下几点:
将ObjectA或ObjectB定义为单个对象或数组
案例1:
只使用ObjectA的定义
{
"X": "test"
}案例2:
只使用ObjectB的定义
{
"Y": "test"
}案例3:
在数组中使用ObjectA或ObjectB的定义
[
{
"X": "test"
},
{
"Y": "test"
}
]案例4:
在数组中使用ObjectA的两次定义
[
{
"X": "test"
},
{
"X": "test"
}
]模式:
我尝试使用这个模式,IntelliSense of MonacoEditor运行良好,但我仍然得到错误/警告:“匹配多个模式,而只有一个必须进行验证。”
{
"definitions": {
"objectA": {
"type": "object",
"properties": {
"X": {
type: "string"
}
}
},
"objectB": {
"type": "object",
"properties": {
"Y": {
type: "string"
}
}
}
},
"oneOf":
[
{
"oneOf":
[
{
"$ref": "#definitions/objectA"
},
{
"$ref": "#definitions/objectB"
}
]
},
{
"type": "array",
"items":
{
"oneOf":
[
{
"$ref": "#definitions/objectA"
},
{
"$ref": "#definitions/objectB"
}
]
}
}
]
}错误/警告:
“当只有一个必须验证时,匹配多个架构。”
发布于 2019-05-13 13:14:12
问题是您在objectA和Y属性en objectB中的X属性是不需要的,因此一个空对象(即{ } )将对这两个属性进行验证。
此外,如果要使用objectA和objectY的数组有效,则需要使用anyOf而不是oneOf。
{
"definitions": {
"objectA": {
"type": "object",
"properties": {
"X": {
"type": "string"
}
},
"required": ["X"]
},
"objectB": {
"type": "object",
"properties": {
"Y": {
"type": "string"
}
},
"required": ["Y"]
}
},
"oneOf":
[
{"$ref": "#/definitions/objectA"},
{"$ref": "#/definitions/objectB"},
{
"type": "array",
"minItems": 1,
"items":
{
"anyOf":
[
{"$ref": "#/definitions/objectA"},
{"$ref": "#/definitions/objectB"}
]
}
}
]
}如果不想要一个空数组来验证,我添加了minItems。
https://stackoverflow.com/questions/56112003
复制相似问题