2016-08-04 45 views
0

我有一個新訂單的JSON模式,它由訂單列表和地址組成。如何驗證所需的JSON模式數組項目

{ 
    "$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" 
    ] 
} 

但它似乎並沒有實際驗證,在所有項目(我使用Laravel 5.2"justinrainbow/json-schema": "~2.0"):

$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

對不起,刪除它。還有更多的驗證,這是一個簡短的例子 – Peon

回答

1

你有混亂的數組和對象類型。您的方案中唯一的數組值必須是order。固定方案:

{ 
    "$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,但在這種情況下,我得到'「數組值發現,但需要一個對象」,因爲'$ input = Request :: input();'創建一個數組like結構 – Peon

+0

在你的數據例子「address」:[]中,但是它會像「address」:{「name」:「Kostya」}這樣的對象,所以你的驗證錯誤也是正確的。我相信所有的困惑是因爲PHP空數組可以表示爲JSON空數組或JSON空對象,但PHP關聯數組只能表示爲JSON對象。 –

+0

謝謝。我認爲這正是Laravel處理請求的方式。我通過在請求後添加'$ input = json_decode(json_encode($ input));'解決了這個問題。 – Peon