2016-10-03 64 views
0

我有這個json模式有一個數組包含多個對象,並且每個對象與其他基於某些模式略有不同。在oneOf之前有可能擁有共同的屬性嗎?

例子。

[ 
    { 
    "ID": "pgID", 
    "Name": "John", 
    "Surname": "Doe", 
    "ProjectsInvolved": [ 
     "My Project", 
     "My Project 2" 
    ] 
    }, 
    { 
    "ID": "jtID", 
    "Name": "John", 
    "Surname": "Doe", 
    "WorksOn": [ 
     "Monday", 
     "Thursday" 
    ] 
    } 
] 

爲JSON的模式是:

{ 
    "$schema": "http://json-schema.org/draft-04/schema", 
    "type": "array", 
    "items": { 
    "oneOf": [ 
     { 
     "type": "object", 
     "properties": { 
      "ID": { 
      "type": "string", 
      "pattern": "^(pg)\\w*$" 
      }, 
      "Name": { 
      "type": "string" 
      }, 
      "Surname": { 
      "type": "string" 
      }, 
      "ProjectsInvolved": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      } 
      } 
     } 
     }, 
     { 
     "type": "object", 
     "properties": { 
      "ID": { 
      "type": "string", 
      "pattern": "^(jt)\\w*$" 
      }, 
      "Name": { 
      "type": "string" 
      }, 
      "Surname": { 
      "type": "string" 
      }, 
      "WorksOn": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      } 
      } 
     } 
     } 
    ] 
    }, 
    "additionalProperties": false 
} 

我的問題是,雖然真正的JSON是相似的,它有更多的項目,並有望隨着更多時間過去增長較大。因此,我必須問,模式是否可以對相同的元素Name和Surname進行分組,並且只有ID和數組在OneOf中?

建議架構的一個例子:

{ 
    "$schema": "http://json-schema.org/draft-04/schema", 
    "type": "array", 
    "items": { 
    "type": "object", 
    "properties": { 
     "Name": { 
     "type": "string" 
     }, 
     "Surname": { 
     "type": "string" 
     }, 
     "oneOf": [ 
     { 
      "ID": { 
      "type": "string", 
      "pattern": "^(pg)\\w*$" 
      }, 
      "ProjectsInvolved": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      } 
      } 
     }, 
     { 
      "ID": { 
      "type": "string", 
      "pattern": "^(jt)\\w*$" 
      }, 
      "WorksOn": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      } 
      } 
     } 
     ] 
    } 
    }, 
    "additionalProperties": false 
} 

回答

1

一般情況下,你要定義的共同的東西前期和之後的特殊條件。這使得模式更易於閱讀併產生更好的錯誤消息。

在本例中,如果存在「ProjectsInvolved」,則「ID」必須以「pg」開頭,並且「WorksOn」不能出現。而且,如果「WorksOn」存在,那麼「ID」必須以「jt」開始並且「ProjectsInvolved」不能出現。

也可以通過oneOfanyOf這樣的東西,但是通常您可以通過dependencies獲得更好的錯誤消息。

{ 
    "$schema": "http://json-schema.org/draft-04/schema", 
    "type": "array", 
    "items": { 
    "type": "object", 
    "properties": { 
     "ID": { "type": "string" }, 
     "Name": { "type": "string" }, 
     "Surname": { "type": "string" }, 
     "ProjectsInvolved": { 
     "type": "array", 
     "items": { "type": "string" } 
     }, 
     "WorksOn": { 
     "type": "array", 
     "items": { "type": "string" } 
     } 
    }, 
    "dependencies": { 
     "ProjectsInvolved": { 
     "properties": { 
      "ID": { "pattern": "^(pg)\\w*$" } 
     }, 
     "not": { "required": ["WorksOn"] } 
     }, 
     "WorksOn": { 
     "properties": { 
      "ID": { "pattern": "^(jt)\\w*$" } 
     }, 
     "not": { "required": ["ProjectsInvolved"] } 
     } 
    } 
    }, 
    "additionalProperties": false 
} 
+0

正是我所需要的!我希望我可以給你一個額外的+1,只是因爲你的名字是Jason,而你正在回答一個與JSON相關的問題。 :d –

相關問題