如何验证 JSON SCHEMA 数组的必需项

How to validate required items of JSON SCHEMA array

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

知道我做错了什么吗?

你搞乱了数组和对象类型。方案中唯一的数组值必须是顺序。固定方案:

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