2016-08-04 10 views
0

私は、注文リストと住所からなる新しい注文用のJSONスキーマを持っています。JSON SCHEMA配列の必須項目を検証する方法

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "array", 
    "properties": { 
    "order": { 
     "type": "array", 
     "items": { 
     "type": "array", 
     "properties": { 
      "product_id": { 
      "type": "integer" 
      }, 
      "quantity": { 
      "type": "integer" 
      } 
     }, 
     "required": [ 
      "product_id", 
      "quantity" 
     ] 
     } 
    }, 
    "address": { 
     "type": "array", 
     "properties": { 
     "name": { 
      "type": "string" 
     }, 
     "phone": { 
      "type": "integer" 
     }, 
     "address1": { 
      "type": "string" 
     }, 
     "address2": { 
      "type": "string" 
     }, 
     "city": { 
      "type": "string" 
     }, 
     "state_or_region": { 
      "type": "string" 
     }, 
     "country": { 
      "type": "string" 
     } 
     }, 
     "required": [ 
     "name", 
     "phone", 
     "address1", 
     "city", 
     "state_or_region", 
     "country" 
     ] 
    } 
    }, 
    "required": [ 
    "order", 
    "address" 
    ] 
} 

しかし、(私が"justinrainbow/json-schema": "~2.0"Laravel 5.2を使用しています)、実際にすべての項目を検証していないよう:

$refResolver = new \JsonSchema\RefResolver(new \JsonSchema\Uri\UriRetriever(), new \JsonSchema\Uri\UriResolver()); 
$schema = $refResolver->resolve(storage_path('schemas\orders.post.json')); 

$errors = []; 
$input = Request::input(); 

// Validate 
$validator = new \JsonSchema\Validator(); 
$validator->check($input, $schema); 

$msg = []; 
if ($validator->isValid()) { 
    return Response::json(['valid'], 200, [], $this->pritify); 
} else { 
    $msg['success'] = false; 
    $msg['message'] = "JSON does not validate"; 
    foreach ($validator->getErrors() as $error) { 
     $msg['errors'][] = [ 
      'error' => ($error['property'] = ' ') ? 'wrong_data' : $error['property'], 
      'message' => $error['message'] 
     ]; 
    } 
    return Response::json($msg, 422, [], $this->pritify); 
} 

このような要求が常に有効来る:

{ 
    "order": [ 
     { 
      "product_id": 100, 
      "quantity": 1 
     }, 
     { 
      "product_id": 0, 
      "quantity": 2 
     } 
    ], 
    "address": [] 
} 

アイデア何が間違っているのですか?

+0

http://jsonlint.com。私のIDEは '" country "' – aynber

+0

の後に角カッコの後ろにカンマをつけてエラーを投げた。申し訳ありませんが、削除しました。 '$ input = Request :: input();'は配列を作成するので、これは検証されるべきことがあります。これは短縮された例です – Peon

答えて

1

あなたは配列型とオブジェクト型が混乱しています。あなたのスキームの唯一の配列値は順序でなければなりません。固定スキーム:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "properties": { 
    "order": { 
     "type": "array", 
     "items": { 
     "type": "object", 
     "properties": { 
      "product_id": { 
      "type": "integer" 
      }, 
      "quantity": { 
      "type": "integer" 
      } 
     }, 
     "required": [ 
      "product_id", 
      "quantity" 
     ] 
     } 
    }, 
    "address": { 
     "type": "object", 
     "properties": { 
     "name": { 
      "type": "string" 
     }, 
     "phone": { 
      "type": "integer" 
     }, 
     "address1": { 
      "type": "string" 
     }, 
     "address2": { 
      "type": "string" 
     }, 
     "city": { 
      "type": "string" 
     }, 
     "state_or_region": { 
      "type": "string" 
     }, 
     "country": { 
      "type": "string" 
     } 
     }, 
     "required": [ 
     "name", 
     "phone", 
     "address1", 
     "city", 
     "state_or_region", 
     "country" 
     ] 
    } 
    }, 
    "required": [ 
    "order", 
    "address" 
    ] 
} 

そして、私はあなたと一緒に持って検証エラーがデータをテスト:

JSON does not validate. Violations: 
[address.name] The property name is required 
[address.phone] The property phone is required 
[address.address1] The property address1 is required 
[address.city] The property city is required 
[address.state_or_region] The property state_or_region is required 
[address.country] The property country is required 
+0

hmmですが、この場合は配列の値が見つかりましたが、 – Peon

+0

あなたのデータの例では "address":[]でも "address":{"name": "Kostya"}のようなオブジェクトになりますので、検証エラーも正しいです。私はすべての混乱は、PHPの空の配列は、JSON空の配列またはJSONの空のオブジェクトとして表すことができますが、PHPの連想配列はJSONオブジェクトとしてのみ表すことができるためです。 –

+0

ありがとうございます。私はこれがLaravelが要求をどのように処理しているかと思います。私はリクエストの後に '$ input = json_decode(json_encode($ input));'を追加することで問題を解決しました。 – Peon

関連する問題