2016-08-15 84 views
0

我的定義如下。它根本不驗證my_field_type_1。可能是什麼原因?我正在使用jsonschema的python模塊。爲什麼jsonschema沒有從模式定義下面的定義?

definitions: 
    TestRouteSchema1: 
    required: 
    - my_field 
    properties: 
     my_field: 
     type: object 
     my_field_type_1: 
      $ref: "#/definitions/MyFieldType1" 
     my_field_type_2: 
      $ref: "#/definitions/MyFieldType2" 
+1

該架構絕對不對,但我不知道你在做什麼。你可以發表你想要驗證的數據的幾個例子嗎?至少有一個應該驗證,另一個不應該驗證。 – Jason

回答

3

您必須嵌套my_field_type_1my_field_type_1下一個新的properties關鍵。所以像

definitions: 
    TestRouteSchema1: 
    required: 
    - my_field 
    properties: 
     my_field: 
     type: object 
     properties: 
      my_field_type_1: 
      $ref: "#/definitions/MyFieldType1" 
      my_field_type_2: 
      $ref: "#/definitions/MyFieldType2" 
+0

是的。我還發現我需要有其他屬性鍵。但我找不到原因?你知道原因嗎? – Rohanil

+2

如果您定義了一個對象類型,那麼屬性定義應該在「屬性」鍵下。這是如何指定json模式。如果您定義了頂級對象類型或嵌套對象類型,它並沒有什麼不同。 – erosb

+0

謝謝,但我想知道爲什麼jsonschema允許我們以錯誤的方式指定模式。在我看來,它應該爲我的模式拋出一些驗證錯誤。 – Rohanil