function ResourceResponseValidator::validateResponse

Same name and namespace in other branches
  1. 8.9.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseValidator.php \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator::validateResponse()
  2. 10 core/modules/jsonapi/src/EventSubscriber/ResourceResponseValidator.php \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator::validateResponse()
  3. 11.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseValidator.php \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator::validateResponse()

Validates a response against the JSON:API specification.

Parameters

\Symfony\Component\HttpFoundation\Response $response: The response to validate.

\Symfony\Component\HttpFoundation\Request $request: The request containing info about what to validate.

Return value

bool FALSE if the response failed validation, otherwise TRUE.

1 call to ResourceResponseValidator::validateResponse()
ResourceResponseValidator::doValidateResponse in core/modules/jsonapi/src/EventSubscriber/ResourceResponseValidator.php
Wraps validation in an assert to prevent execution in production.

File

core/modules/jsonapi/src/EventSubscriber/ResourceResponseValidator.php, line 131

Class

ResourceResponseValidator
Response subscriber that validates a JSON:API response.

Namespace

Drupal\jsonapi\EventSubscriber

Code

protected function validateResponse(Response $response, Request $request) {
    // If the validator isn't set, then the validation library is not installed.
    if (!$this->validator) {
        return TRUE;
    }
    // Do not use Json::decode here since it coerces the response into an
    // associative array, which creates validation errors.
    $response_data = json_decode($response->getContent());
    if (empty($response_data)) {
        return TRUE;
    }
    $schema_ref = sprintf('file://%s/schema.json', implode('/', [
        $this->appRoot,
        $this->moduleHandler
            ->getModule('jsonapi')
            ->getPath(),
    ]));
    $generic_jsonapi_schema = (object) [
        '$ref' => $schema_ref,
    ];
    return $this->validateSchema($generic_jsonapi_schema, $response_data);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.