gpt4 book ai didi

json - z模式中的 "anyof"和 "oneof"有什么区别?

转载 作者:行者123 更新时间:2023-12-02 03:02:03 25 4
gpt4 key购买 nike

看起来两者都可以很好地处理我的输入验证代码。那么具体的区别是什么呢?

具有 oneof 的架构

[{
"id": "MyAction",
"oneOf": [{ "$ref": "A1" },
{ "$ref": "A2" }]
},
{
"id": "A1",
"properties": {
"class1": { "type": "string"},
"class2": { "type": "string"}
}
},
{
"id": "A2",
"properties": {
"class2": { "type": "string"},
"class3": { "type": "string"}
}
}
]

架构与任何

    [{
"id": "MyAction",
"anyOf": [{ "$ref": "A1" },
{ "$ref": "A2" }]
},
{
"id": "A1",
"properties": {
"class1": { "type": "string"},
"class2": { "type": "string"}
}
},
{
"id": "A2",
"properties": {
"class2": { "type": "string"},
"class3": { "type": "string"}
}
}
]

最佳答案

如果您查看 JSON Schema 文档,它会显示:

anyOf :

...

An instance validates successfully against this keyword if itvalidates successfully against at least one schema defined by thiskeyword's value. Note that when annotations are being collected, allsubschemas MUST be examined so that annotations are collected fromeach subschema that validates successfully.

oneOf :

...

An instance validates successfully against this keyword if itvalidates successfully against exactly one schema defined by thiskeyword's value.

请注意我在上面的强调。 anyOf 表示该项目必须针对至少一个(但可能多个)模式进行验证。 oneOf 表示它必须仅针对其中一个模式进行验证。

关于json - z模式中的 "anyof"和 "oneof"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34038183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com