2015-10-02 23 views
0

我想在我的模式中使用數組(項目)。數組中的每個對象可以是架構中概述的「幀類型」之一。JSON模式 - 在項目數組中使用oneOf

我開發的模式下面給出:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "required": [ 
    "Campaign_name", 
    "Legals" 
    ], 
    "properties": { 
    "Campaign_name": { 
    "type": "string", 
    "minLength": 3 
    }, 
    "Legals": { 
    "type": "object", 
    "properties": { 
    "legal-button-label": { 
     "type": "string", 
     "minLength": 6 
    }, 
    "legal-text": { 
     "type": "string", 
     "minLength": 15 
    } 
    } 
}, 
"Banner_120x600": { 
    "type": "object", 
    "properties": { 
    "serve-backup": { 
     "type": "object", 
     "properties": { 
     "choice": { 
      "type": "string", 
      "enum": [ 
      "yes", 
      "no" 
      ] 
     }, 
     "image": { 
      "type": "string", 
      "pattern": "^([a-zA-Z|-]+)([.])(gif|jpeg|jpg|png)$" 
     } 
     } 
    }, 
    "background": { 
     "type": "string", 
     "pattern": "^([a-zA-Z|-]+)([.])(gif|jpeg|jpg|png)$" 
    }, 
    "logo": { 
     "type": "string", 
     "pattern": "^([a-zA-Z|-]+)([.])(gif|jpeg|jpg|png)$" 
    }, 
    "loop": { 
     "type": "integer", 
     "enum": [ 
     0, 
     1, 
     2 
     ] 
    }, 
    "frames": { 
     "type": "array", 
     "minItems": 1, 
     "maxItems": 6, 
     "items": { 
     "oneOf": [ 
      { 
      "$ref": "#/frame-type/INTRO-FRAME" 
      }, 
      { 
      "$ref": "#/frame-type/OFFER-FRAME-TYPE-1" 
      } 
     ] 
     } 
    } 
    } 
} 
}, 
"frame-type": { 
    "INTRO-FRAME": {}, 
    "OFFER-FRAME-TYPE-1": {} 
    } 
} 

然而,JSON沒有根據模式驗證。我正在開發的JSON是如下:

{ 
    "Campaign_name": "OSM DT DATA", 
    "Legals": { 
    "legal-button-label": "Click for Legals", 
    "legal-text": "Requires 3G/Wi-Fi. Content depends..." 
    }, 
    "Banner_120x600": { 
    "serve-backup": { 
    "choice": "no", 
    "image": "backup.jpg" 
}, 
"background": "background.png", 
"logo": "sky-logo.png", 
"loop": 2, 
"frames": [ 
    { 
    "type": "INTRO-FRAME" 
    }, 
    { 
    "type": "OFFER-FRAME-TYPE-1" 
    } 
] 
} 
} 

回答

0
"oneOf": [ 
     { 
     "$ref": "#/frame-type/INTRO-FRAME" 
     }, 
     { 
     "$ref": "#/frame-type/OFFER-FRAME-TYPE-1" 
     } 
    ] 

兩個achemas是空的,oneOf失敗,如果一個對象,因爲任何文件的空模式匹配匹配多個模式,這將永遠是這樣。您可以將oneOf更改爲anyOf。

+0

我填充了模式,它仍然無法驗證。 – Berni

+0

你是如何填充它們的?你如何驗證它? –

+0

嗨,我發現了一個不使用oneOf的解決方案。我會更新問題並回答它。感謝您的時間和建議。 – Berni

0
"frames": { 
     "type": "array", 
     "minItems": 1, 
     "items": [ 
      { 
       "type" : "object", 
       "properties" : { 
        "type" : { "enum" : ["INTRO-FRAME"] } 
       } 
      }, 
      { 
       "type" : "object", 
       "properties" : { 
        "type" : { "enum" : ["OFFER-FRAME-TYPE-1"] }  
       } 
      }, 
      { 
       "type" : "object", 
       "properties" : { 
        "type" : { "enum" : ["OFFER-FRAME-TYPE-2"] }  
       } 
      }, 
      { 
       "type" : "object", 
       "properties" : { 
        "type" : { "enum" : ["CONTENT-FRAME"] } 
       } 
      } 
     ] 
    } 

我刪除了「oneOf」的使用並提供了一個要驗證的對象數組。

相關問題