如何验证JSON SCHEMA数组

时间:2016-08-04 13:10:12

标签: php json laravel jsonschema

我有新订单的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": []
}

任何想法我做错了什么?

1 个答案:

答案 0 :(得分: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