首页 > 解决方案 > json模式中的多个条件必填字段

问题描述

如果枚举匹配某个值,我有一个具有所需属性的架构。这工作正常。

    "oneOf": [
      {
        "not" : {
          "properties": {
            "other-val": { "enum" : ["ABC"]}
          }
        }
      },
      { "required": ["ABC-detail"] } 
    ],

但是,我正在尝试添加第二个字段,DEF-detail基于other-value枚举是否为“DEF”。我不确定如何将这些依赖项链接起来。我尝试将多个oneOfs 放入其中allOf,但我无法向其中添加多个相同键。

标签: jsonjsonschema

解决方案


作品中的一组条件oneOf。但我不认为这排除了 aDEFabc-thing情况,例如,但这在我的情况下是可以的。

    "oneOf": [
      {
        "properties": {"other-val": {"enum": ["ABC"]}},
        "required": ["abc-thing"]
      },
      {
        "properties": {"other-val": {"enum": ["DEF"]}},
        "required": ["def-thing"]
      },  
      {
        "properties": {"other-val": {"enum": ["GHI"]}},
        "required": ["ghi-thing"]
      },            
      {
        "not": {"properties": {"other-val": {"enum": ["ABC", "DEF", "GHI"]}}}
      }
    ],

推荐阅读