我数据中的一个字段如下:
{
"license":
"url": "<some url>",
"label": "<some label>"
}
例如,如果用户提供“ url”的两个值之一,我想验证这一点:
["http://creativecommons.org/licenses/by/4.0/",
"https://creativecommons.org/licenses/by/4.0/"]
标签的值必须是以下之一:
["CC-BY", "CC BY 4.0", "CC-BY 4.0"]
并且有多个不同的标签选项,支持HTTP或HTTPS。我尝试了以下操作,但是验证失败(无法验证),并且我找不到与相应值有关的任何内容,只是如果一个字段存在,那么另一个字段必须存在(依赖项)。>
"license": {
"type": "object",
"properties": {
"oneOf": [
{
"url": { "enum": ["http://creativecommons.org/licenses/by/4.0/", "https://creativecommons.org/licenses/by/4.0/"] },
"label": { "enum": ["CC-BY", "CC BY 4.0", "CC-BY 4.0"] }
},
{
"url": { "enum": ["http://creativecommons.org/publicdomain/zero/1.0/", "https://creativecommons.org/publicdomain/zero/1.0/"] },
"label": { "enum": ["CC-0", "CC0", "CC0 1.0 Universal", "CC0 1.0"] }
},
{
"url": { "enum": ["http://creativecommons.org/licenses/by/3.0/", "https://creativecommons.org/licenses/by/3.0/"] },
"label": { "enum": ["CC-BY", "CC-BY 3.0"] }
}
... <and so on>
我在oneOf
中尝试了几次不同的依赖项/属性迭代,但似乎没有任何效果。
答案 0 :(得分:0)
我发现有效的方法-"oneOf"
应该与"properties"
齐平,并在其中封装"properties"
对象,如下所示。
"license": {
"type": "object",
"properties": {
"url": { "type": "string", "format": "uri" },
"label": { "type": "string" },
"logo": { "type": "string", "format": "uri" }
},
"required": ["url", "label"],
"oneOf": [
{
"properties": {
"url": { "enum": ["http://creativecommons.org/licenses/by/4.0/", "https://creativecommons.org/licenses/by/4.0/"] },
"label": { "enum": ["CC-BY", "CC BY 4.0", "CC-BY 4.0"] }
}
},
{
"properties": {
"url": { "enum": ["http://creativecommons.org/publicdomain/zero/1.0/", "https://creativecommons.org/publicdomain/zero/1.0/"] },
"label": { "enum": ["CC-0", "CC0", "CC0 1.0 Universal", "CC0 1.0"] }
}
},
{
"properties": {
"url": { "enum": ["http://creativecommons.org/licenses/by/3.0/", "https://creativecommons.org/licenses/by/3.0/"] },
"label": { "enum": ["CC-BY", "CC-BY 3.0"] }
}
},
... <and so on>