MediaType应该是application / schema + json来验证模式

时间:2015-02-03 15:41:04

标签: json jsonschema json-schema-validator

我正在使用justinrainbow / json-schema类来根据模式验证数据。 但是我收到了这个错误:

Media type application/schema+json expected

我可以尝试在我的所有json文件中更改nginx中的ContentType,但它没有意义。

另一种方法是将库中的常量更改为'application / json'(因为我的服务器正在为json文件提供)。再一次,改变消息来源是不行的。

有没有办法将此作为参数传递给justinrainbow / json-schema类?

https://github.com/justinrainbow/json-schema

2 个答案:

答案 0 :(得分:0)

我无法找到解决方案,因为网络上没有内容类型作为schema + json。

只需在justinrainbow / json-schema / src / JsonSchema / Validator.php中将SCHEMA_MEDIA_TYPE替换为' application / json'。

您也可以按本地路径提供文件,而不是按网址提供。

答案 1 :(得分:0)

现在该库还支持“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:json解码来自API的响应

$ schema:架构的url

在针对其架构测试其他方的API时,我多次遇到同样的问题。他们通常不会为他们的模式发送正确的“Content-Type”标头,并且可能需要很长时间才能更改它。