JSON模式:如何定义某些属性组合是必需的

时间:2014-08-13 21:02:17

标签: json jsonschema

例如,有2个json属性X和Y,每个属性都是可选的,但其中一个必须是X或Y.如何定义这样的json模式?

1 个答案:

答案 0 :(得分:0)

您可以使用oneOf指定所需属性的可能组合。另请参阅How to make json-schema to allow one but not another field?

"oneOf": [
  { 
    "properties": {
      "A": {"$ref": "aType"},
      "B": {"$ref": "bType"},          
      "X": {"$ref": "xType"}
    }      
    "required": ["A", "B", "X" ],
    "additionalProperties": false
  },
  { 
    "properties": {
      "A": {"$ref": "aType"},
      "B": {"$ref": "bType"},          
      "Y": {"$ref": "yType"}
    }      
    "required": ["A", "B", "Y" ],
    "additionalProperties": false
  }
]