2017-06-19 151 views
0

我正在创建JsonSchema(v4)。 我试图根据其父项中的另一个属性的值来创建一个所需的属性。json模式 - 根据另一个字段值需要字段

  • 用户
    • 亚型
    • 地址

儿童

  • 地址
    • 一号线
    • 2号线
    • 的companyName(如果需要用户亚型公司)

怎么能这样做? 我有这样的事情现在......

{ 
"User": { 
    "title": "User", 
    "type": "object", 
    "id": "#User", 
    "properties": { 
    "subtype": { 
     "type": "string" 
    }, 
    "address": { 
     "$ref": "Address" 
    } 
    } 
} 


"Address": { 
    "title": "Address", 
    "type": "object", 
    "id": "#Address", 
    "properties": { 
    "line1": { 
     "type": "string" 
    }, 
    "line2": { 
     "type": "string" 
    }, 
    "companyName": { 
     "type": "string" 
    } 
    }, 
    "required": ["line1", "line2"] 
} 
} 

亚型是一个任意字符串,所以不同亚型的完整列表是不可能的。

回答

0

将此添加到您的用户架构。基本上它读作:“子类型”不是“公司”或“地址”需要“公司名称”。

"anyOf": [ 
    { 
    "not": { 
     "properties": { 
     "subtype": { "enum": ["company"] } 
     } 
    } 
    }, 
    { 
    "properties": { 
     "address": { 
     "required": ["companyName"] 
     } 
    } 
    } 
] 
+0

非常感谢你这么多! – neljamin

相关问题