From d868bbac6667fa51e1993bf80075c0021cb958a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Sun, 6 Dec 2020 13:23:12 +0100 Subject: [PATCH] Fix #96 Add support for providing a custom schema path to ResponseValidator --- CHANGELOG.md | 1 + src/JsonApi/Negotiation/AbstractMessageValidator.php | 11 +++++++++-- src/JsonApi/Negotiation/ResponseValidator.php | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a7a7657..b1d0c2b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ADDED: - [#98](https://github.com/woohoolabs/yin/issues/98): Support for validating top-level members in requests +- [#96](https://github.com/woohoolabs/yin/issues/96): Support for providing a custom schema path to `ResponseValidator` CHANGED: diff --git a/src/JsonApi/Negotiation/AbstractMessageValidator.php b/src/JsonApi/Negotiation/AbstractMessageValidator.php index b5dc475b..be2b6657 100644 --- a/src/JsonApi/Negotiation/AbstractMessageValidator.php +++ b/src/JsonApi/Negotiation/AbstractMessageValidator.php @@ -24,12 +24,19 @@ abstract class AbstractMessageValidator */ protected $includeOriginalMessage; + /** + * @var string|null + */ + protected $customSchemaPath; + public function __construct( ExceptionFactoryInterface $exceptionFactory, - bool $includeOriginalMessageInResponse = true + bool $includeOriginalMessageInResponse = true, + ?string $customSchemaPath = null ) { $this->exceptionFactory = $exceptionFactory; $this->includeOriginalMessage = $includeOriginalMessageInResponse; + $this->customSchemaPath = $customSchemaPath; } protected function validateJsonMessage(string $message): string @@ -59,7 +66,7 @@ protected function validateJsonApiMessage(string $message): array $validator = new Validator(); $validator->validate( $decodedMessage, - (object) ['$ref' => "file://" . realpath(__DIR__ . "/json-api-schema.json")] + (object) ['$ref' => "file://" . ($this->customSchemaPath ?? realpath(__DIR__ . "/json-api-schema.json"))] ); return $validator->getErrors(); diff --git a/src/JsonApi/Negotiation/ResponseValidator.php b/src/JsonApi/Negotiation/ResponseValidator.php index 63420837..90095937 100644 --- a/src/JsonApi/Negotiation/ResponseValidator.php +++ b/src/JsonApi/Negotiation/ResponseValidator.php @@ -21,9 +21,10 @@ class ResponseValidator extends AbstractMessageValidator public function __construct( SerializerInterface $deserializer, ExceptionFactoryInterface $exceptionFactory, - bool $includeOriginalMessageInResponse = true + bool $includeOriginalMessageInResponse = true, + ?string $customSchemaPath = null ) { - parent::__construct($exceptionFactory, $includeOriginalMessageInResponse); + parent::__construct($exceptionFactory, $includeOriginalMessageInResponse, $customSchemaPath); $this->deserializer = $deserializer; }