2016-11-07 72 views
0

這是我的要求:當num == 0時,a是必需的;當num == 1時,b是必需的。如何爲屬性編寫JSON模式取決於其他字段的值?

{ 
    "type": "object", 
    "properties": { 
    "num": { "type": "integer" }, 
    "a": { "type": "string" }, 
    "b": { "type": "string" } 
    } 
    "required": [ "num" ] 
} 
+0

什麼你試試? –

+0

我還沒有找到值依賴的解決方案。 – Hom

回答

0

我目前的解決方案:

{ 
    "type": "object", 
    "oneOf": [ 
    {"$ref": "#/definitions/0"}, 
    {"$ref": "#/definitions/1"}, 
    ], 
    "definitions": { 
    "0": { 
     "properties": { 
     "num": {"enum": [0]}, 
     "a": {"type": "string"} 
     }, 
     "required": ["num", "a"] 
    }, 
    "1": { 
     "properties": { 
     "num": {"enum": [1]}, 
     "b": {"type": "string"} 
     }, 
     "required": ["num", "b"] 
    } 
    } 
} 

我使用jsonschema,它返回is not exactly on from <#definitions/0>。這不是我想要的。我希望它可以返回有關細節的消息,如a是必需的。

0

改進版本:

{ 
    "type": "object", 
    "properties": { 
    "num": { "type": "integer" }, 
    "a": { "type": "string" }, 
    "b": { "type": "string" } 
    } 
    "required": [ "num" ], 
    "dependencies": { 
    "a": { 
     "properties": { 
     "num": { 
      "enum": [0] 
     } 
     } 
    }, 
    "b": { 
     "properties": { 
     "num": { 
      "enum": [1] 
     } 
     } 
    } 
    } 
} 
相關問題