2013-07-29 26 views
13

私はオブジェクトのjson-schema配列にrequiredを設定する方法を理解しようとしています。必要なプロパティは、配列ではないオブジェクトに対して正常に機能します。ここでjson-schema配列に設定する必要があります

は私のJSONスキーマの項目の一部である:ここで

 "items": { 
     "type": "array", 
     "properties": { 
      "item_id": {"type" : "number"}, 
      "quantity": {"type": "number"}, 
      "price": {"type" : "decimal"}, 
      "title": {"type": "string"}, 
      "description": {"type": "string"} 
     }, 
     "required": ["item_id","quantity","price","title","description"], 
     "additionalProperties" : false 
    } 

は、私がオーバー送りますJSON配列です。私はこれらの項目に説明を渡していないので、json検証は失敗するはずです。

 "items": [ 
     { 
      "item_id": 1, 
      "quantity": 3, 
      "price": 30, 
      "title": "item1 new name" 
     }, 
     { 
      "item_id": 1, 
      "quantity": 16, 
      "price": 30, 
      "title": "Test Two" 
     } 
    ] 

答えて

15

itemsのオブジェクト内の配列要素のスキーマの一部を入れ子にして、this validatorを使って動作させました。スキーマは現在2つの入れ子itemsのフィールドを持っていますが、1がJSONSchemaのキーワードやその他があるので、それがある、あなたのJSONは、実際にitems

JSONSchemaと呼ばれるフィールドを持っているので:

{ 
    "type":"object", 
    "properties":{ 
     "items":{ 
     "type":"array", 
     "items":{ 
      "properties":{ 
       "item_id":{ 
        "type":"number" 
       }, 
       "quantity":{ 
        "type":"number" 
       }, 
       "price":{ 
        "type":"number" 
       }, 
       "title":{ 
        "type":"string" 
       }, 
       "description":{ 
        "type":"string" 
       } 
      }, 
      "required":[ 
       "item_id", 
       "quantity", 
       "price", 
       "title", 
       "description" 
      ], 
      "additionalProperties":false 
     } 
     } 
    } 
} 

JSON:

{ 
    "items":[ 
     { 
     "item_id":1, 
     "quantity":3, 
     "price":30, 
     "title":"item1 new name" 
     }, 
     { 
     "item_id":1, 
     "quantity":16, 
     "price":30, 
     "title":"Test Two" 
     } 
    ] 
} 

説明フィールドが欠落して約2つのエラーのある出力:

[ { 
    "level" : "error", 
    "schema" : { 
    "loadingURI" : "#", 
    "pointer" : "/properties/items/items" 
    }, 
    "instance" : { 
    "pointer" : "/items/0" 
    }, 
    "domain" : "validation", 
    "keyword" : "required", 
    "message" : "missing required property(ies)", 
    "required" : [ "description", "item_id", "price", "quantity", "title" ], 
    "missing" : [ "description" ] 
}, { 
    "level" : "error", 
    "schema" : { 
    "loadingURI" : "#", 
    "pointer" : "/properties/items/items" 
    }, 
    "instance" : { 
    "pointer" : "/items/1" 
    }, 
    "domain" : "validation", 
    "keyword" : "required", 
    "message" : "missing required property(ies)", 
    "required" : [ "description", "item_id", "price", "quantity", "title" ], 
    "missing" : [ "description" ] 
} ] 

hereに上記を貼り付けて、同じ出力が生成されていることを確認してください。

5

多分あなたのバリデーターはJSONSchema v3のみをサポートしていますか?

required作品はV3とV4との間で変更方法:

+0

json-schema.orgはv4を使用しているようです。 – ipengineer

4

私は、これは古いスレッドです実現しますが、この質問はjsonschema.netからリンクされているので、私はそれがで鳴る価値があるかもしれないと思った...

あなたの元の例の問題点は、宣言しているということです配列の "items"を宣言し、配列に値を設定する "オブジェクト"型( "プロパティ"を持つ)を宣言するのではなく、 "配列"型の "プロパティ"を使用します。ここでは、元のスキーマの抜粋の改訂版です:

"items": { 
    "type": "array", 
    "items": { 
     "type": "object", 
     "properties": { 
      "item_id": {"type" : "number"}, 
      "quantity": {"type": "number"}, 
      "price": {"type" : "decimal"}, 
      "title": {"type": "string"}, 
      "description": {"type": "string"} 
     }, 
     "required": ["item_id","quantity","price","title","description"], 
     "additionalProperties" : false 
    } 
} 

私は混乱を避けるために、配列の名前のための用語「アイテム」を使用しないことをお勧めしますが、それをやってからあなたを止めるものは何もありません...

関連する問題