2017-06-08 18 views
1

我现在的JSON模式定义是这样的JSON模式使产权条件必需

{ 
    "properties": { 
    "account_type": { 
     "description": "account type", 
     "enum": [ 
     "CURRENT", 
     "SAVINGS", 
     "DEMAT" 
     ], 
     "type": "string" 
    }, 
    "demat_account_number": { 
     "description": "demat_account_number", 
     "type": "string" 
    } 
    }, 
    "required": [ 
    "account_type" 
    ], 
    "type": "object" 
} 

我的要求是,如果“ACCOUNT_TYPE” =“DEMAT”,然后“demat_account_number”应该成为一个必需的属性。

有什么办法可以实现这种验证?

回答

1

您可以使用“oneOf”。这强制符合文件以实施多种可能模式中的仅一种:

{ 
    "oneOf":[ 
     { 
      "properties":{ 
       "account_type":{ 
        "description":"account type", 
        "enum":[ 
         "CURRENT", 
         "SAVINGS" 
        ], 
        "type":"string" 
       } 
      }, 
      "required":[ 
       "account_type" 
      ], 
      "type":"object" 
     }, 
     { 
      "properties":{ 
       "account_type":{ 
        "description":"account type", 
        "enum":[ 
         "DEMAT" 
        ], 
        "type":"string" 
       }, 
       "demat_account_number":{ 
        "description":"demat_account_number", 
        "type":"string" 
       } 
      }, 
      "required":[ 
       "account_type", 
       "demat_account_number" 
      ], 
      "type":"object" 
     } 
    ] 
} 
+0

作品...谢谢! –