2017-06-22 79 views
0

我有輸入JSON像下面,AJV架構驗證錯誤

{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]} 

的條件是, 如果類型是「場」,然後「ITEMID」應該是必需字段 並且如果類型是「FIELDGROUP」或「款」,然後「ITEMID」是可選的

這是JSON模式我試着和它的工作不正常,

"type": "object", 
"additionalProperties": false, 
"properties" : { 
    "contents" : { 
     "type" : "array", 
     "items": {"$ref": "#displayItem" } 
    } 
}, 
"definitions": { 
    "displayItem" : { 
     "id": "#displayItem", 
     "type": "object", 
     "items": { 
      "anyOf": [ 
       {"$ref": "#fieldType"}, 
       {"$ref": "#fieldGroupSubSectionType"} 
      ] 
     } 
    }, 

    "fieldType" : { 

     "id": "#fieldType", 
     "type": "object", 
     "additionalProperties": false, 
     "properties": { 
      "itemId": { 
       "type": "string" 
      }, 
      "type": { 
       "type": "string", 
       "enum": ["field"] 
      } 
     } 

    }, 

    "fieldGroupSubSectionType" : { 

     "id": "#fieldGroupSubSectionType", 
     "type": "object", 
     "additionalProperties": false, 
     "properties": { 
      "itemId": { 
       "type": [ "string", "null" ] 
      }, 
      "type": { 
       "type": "string", 
       "enum": [ 
        "fieldGroup", 
        "subSection" 
       ] 
      } 
     } 

    } 
} 

任何幫助/解決方法w^ith示例Json Schema來實現上述用例,我們感激不盡。

回答

0

如果我理解了你想要的東西的描述,那麼你提供的json例子是無效的,因爲它有一個類型:「field」但沒有「itemId」屬性。

假設這是真的。除了使用

類型的: 「串」,無效]

使用需要財產。

我改變你的架構了一下,而不必單獨的定義我內聯它們,但除此之外,(和使用要求的)是一樣的:

{ 
    "type": "object", 
    "additionalProperties": false, 
    "properties": { 
    "contents": { 
     "type": "array", 
     "items": { 
     "anyOf": [ 
      { 
      "type": "object", 
      "additionalProperties": false, 
      "properties": { 
       "itemId": { 
       "type": "string" 
       }, 
       "type": { 
       "type": "string", 
       "enum": [ 
        "field" 
       ] 
       } 
      }, 
      "required": [ 
       "itemId" 
      ] 
      }, 
      { 
      "type": "object", 
      "additionalProperties": false, 
      "properties": { 
       "itemId": { 
       "type": "string" 
       }, 
       "type": { 
       "type": "string", 
       "enum": [ 
        "fieldGroup", 
        "subSection" 
       ] 
       } 
      } 
      } 
     ] 
     } 
    } 
    } 
} 
+0

謝謝佩德羅。有效**。 – Ragubathy

0

下面是一些清理你的答案獲得最佳做法和風格。訣竅是你需要使用暗示「暗示b < =>(不是a)或b」。在這種情況下,你有「type =字段意味着itemId是必需的< =>類型不是字段或itemId是必需的」。

{ 
    "type": "object", 
    "properties": { 
    "contents": { 
     "type": "array", 
     "items": { "$ref": "#/definitions/displayItem" } 
    } 
    }, 
    "definitions": { 
    "displayItem": { 
     "type": "object", 
     "properties": { 
     "itemId": { "type": "string" }, 
     "type": { "enum": ["field", "fieldGroup", "subSection"] } 
     }, 
     "anyOf": [ 
     { "not": { "$ref": "#/definitions/fieldType" } }, 
     { "required": ["itemId"] } 
     ] 
    }, 
    "fieldType": { 
     "properties": { 
     "type": { "enum": ["field"] } 
     } 
    } 
    } 
}