2017-06-08 53 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

作品...謝謝! –