2017-04-12 70 views
0

我一直在閱讀如何定義json架構以容納不同形狀對象的列表:人,地址,車輛。部分研究使我找到了有用的補充建議。例如添加「uniqueitems」:true,以便列表不包含重複項。http://www.jsonschemavalidator.net/不會出現要兌現「uniqueitems」:true或「additionalProperties」:false

我發現該網站http://www.jsonschemavalidator.net/非常有用的驗證我的架構和JSON數據,但我不能找出我在做什麼毛病都

"additionalProperties": false 

"uniqueitems": true 

這裏是我的示例架構和數據:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "an array of Insurable Items", 
    "type": "array", 
    "items": { 
    "type": "object", 
    "OneOf": [ 
     { 
     "type": "object", 
     "description": "A Person", 
     "properties": { 
      "person": { 
      "type": "object", 
      "$ref": "#/definitions/person" 
      } 
     }, 
     "required": [ 
      "person" 
     ], 
     "additionalProperties": false 
     }, 
     { 
     "type": "object", 
     "description": "An Address", 
     "properties": { 
      "address": { 
      "type": "object", 
      "$ref": "#/definitions/address" 
      } 
     }, 
     "required": [ 
      "address" 
     ], 
     "additionalProperties": false 
     }, 
     { 
     "type": "object", 
     "description": "A Vehicle", 
     "properties": { 
      "vehicle": { 
      "type": "object", 
      "$ref": "#/definitions/vehicle" 
      } 
     }, 
     "required": [ 
      "vehicle" 
     ], 
     "additionalProperties": false 
     } 
    ], 
    "uniqueitems": true 
    }, 

    "definitions": { 
    "person": { 
     "type": "object", 
     "properties": { 
     "id": { 
      "type": "string" 
     }, 
     "title": { 
      "type": "string" 
     }, 
     "firstname": { 
      "type": "string" 
     }, 
     "lastname": { 
      "type": "string" 
     }, 
     "dateofbirth": { 
      "type": "string" 
     }, 
     "employmentstatus": { 
      "type": "string" 
     }, 
     "occupation": { 
      "type": "string" 
     } 
     }, 
     "additionalProperties": false 
    }, 
    "address": { 
     "type": "object", 
     "properties": { 
     "id": { 
      "type": "string" 
     }, 
     "line1": { 
      "type": "string" 
     }, 
     "line2": { 
      "type": "string" 
     }, 
     "line3": { 
      "type": "string" 
     }, 
     "line4": { 
      "type": "string" 
     }, 
     "line5": { 
      "type": "string" 
     }, 
     "postcode": { 
      "type": "string" 
     } 
     }, 
     "additionalProperties": false 
    }, 
    "vehicle": { 
     "type": "object", 
     "properties": { 
     "id": { 
      "type": "string" 
     }, 
     "vehiclecode": { 
      "type": "string" 
     }, 
     "registrationyear": { 
      "type": "string" 
     }, 
     "registrationletter": { 
      "type": "string" 
     }, 
     "registrationnumber": { 
      "type": "string" 
     }, 
     "description": { 
      "type": "string" 
     } 
     }, 
     "additionalProperties": false 
    } 
    } 
} 

數據:

[ 
    { 
    "person": { 
     "id": "123456789-01", 
     "title": "Mr", 
     "firstname": "Joe", 
     "lastname": "blogs" 
    }, 
    "badadditional": "thing" 
    }, 
    { 
    "address": { 
     "id": "123456789-A", 
     "line1": "1 The Mall", 
     "line2": "Westminster", 
     "line3": "London", 
     "postcode": "SW1A 1AA" 
    } 
    }, 
    { 
    "address": { 
     "id": "123456789-A", 
     "line1": "1 The Mall", 
     "line2": "Westminster", 
     "line3": "London", 
     "postcode": "SW1A 1AA" 
    } 
    }, 
    { 
    "vehicle": { 
     "id": "123456789-01-01", 
     "vehiclecode": "string", 
     "registrationyear": "string", 
     "registrationletter": "string", 
     "registrationnumber": "string", 
     "description": "string", 
     "badadditional": "other thing" 
    } 
    } 
] 

如果您複製這些到http://www.jsonschemavalidator.net/架構驗證,它儘管

"badadditional": "thing" 

"badadditional": "other thing" 

存在和重複地址對象報告沒有問題。

我一直在尋找stackoverflow [標籤爲json.net],閱讀(其中),http://grokbase.com,http://json-schema.org,我正在努力草案-04。

我也試過https://jsonschemalint.com/#/version/draft-04/markup/json但也沒有抱怨。

任何人都可以指向其他驗證器,json架構文檔和示例,或者讓我知道我做錯了什麼,如果它很明顯?

回答

2

additionalProperties工作得很好。問題是您已使用OneOf而不是oneOf。 JSON模式關鍵字區分大小寫,所以驗證器不能識別該關鍵字,並忽略它和它定義的所有內容。

你有兩個問題uniqueItems。首先是另一個區分大小寫的問題。您已使用uniqueitems。另一個問題是它在錯誤的地方。它是items模式(它是一個對象)的一部分,當它應該是根模式(這是一個數組)的一部分時。

因此,修復您的大小寫並將uniqueItems向上移動一級,它應該都按預期工作。

+0

謝謝傑森。我希望這是我做的一些愚蠢的事情。現在我再看資源,他們顯然是區分大小寫的! –

+0

這是一種享受 - 謝謝 –