现在该库还支持“json/application”,但它会在其他内容类型上引发错误。
为避免这种情况,您可以扩展默认的“JsonSchema\Uri\UriRetriever”并覆盖“ confirmMediaType() ”:
class MyUriRetriever extends JsonSchema\Uri\UriRetriever {
public function confirmMediaType($uriRetriever, $uri) {
return;
}
}
$retriever = new \MyUriRetriever();
$refResolver = new JsonSchema\SchemaStorage($retriever);
$schema = $refResolver->resolveRef($schema);
$validator = new JsonSchema\Validator(new JsonSchema\Constraints\Factory($refResolver));
$validator->check($data, $schema);
$data:来自 API 的 json 解码响应
$schema:架构的 url
在针对他们的架构测试其他方的 API 时,我多次遇到同样的问题。他们通常不会为他们的模式发送正确的“Content-Type”标头,而且他们可能需要很长时间才能更改它。
更新:能够从验证中排除端点
您可以使用UriRetriever:addInvalidContentTypeEndpoint():
$retriever = new UriRetriever();
$retriever->addInvalidContentTypeEndpoint('http://example.com/car/list');