Skip to content

Commit

Permalink
Corona: JSON validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Dec 11, 2024
1 parent e97166a commit 942d7d9
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions src/Lunr/Corona/JsonView.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

namespace Lunr\Corona;

use _PHPStan_c875e8309\Psr\Container\ContainerInterface;

Check failure on line 13 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Referencing prefixed PHPStan class: _PHPStan_c875e8309\Psr\Container\ContainerInterface.
use JsonSchema\RefResolver;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;
use Lunr\Core\Configuration;
use stdClass;
use Throwable;
Expand All @@ -19,17 +23,50 @@
*/
class JsonView extends View
{
/**
* Shared instance of the JSON URI retriever
* @var UriRetriever
*/
protected $jsonretriever;

Check failure on line 30 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Property Lunr\Corona\JsonView::$jsonretriever has unknown class JsonSchema\Uri\UriRetriever as its type.

/**
* Shared instance of the JSON resolver
* @var RefResolver
*/
protected $jsonresolver;

Check failure on line 36 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Property Lunr\Corona\JsonView::$jsonresolver has unknown class JsonSchema\RefResolver as its type.

/**
* Shared instance of the JSON validator
* @var Validator
*/
protected $jsonvalidator;

Check failure on line 42 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Property Lunr\Corona\JsonView::$jsonvalidator has unknown class JsonSchema\Validator as its type.

/**
* The list of calls to check and what to check them against
* @var array
*/
protected array $allowlist = [];

/**
* Constructor.
*
* @param Request $request Shared instance of the Request class
* @param Response $response Shared instance of the Response class
* @param Configuration $configuration Shared instance of the Configuration class
* @param Request $request Shared instance of the Request class
* @param Response $response Shared instance of the Response class
* @param Configuration $configuration Shared instance of the Configuration class
* @param ContainerInterface|null $locator Shared instance of the ContainerInterface class
*/
public function __construct($request, $response, $configuration)
public function __construct($request, $response, $configuration, $locator = NULL)

Check failure on line 58 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Parameter $locator of method Lunr\Corona\JsonView::__construct() has invalid type _PHPStan_c875e8309\Psr\Container\ContainerInterface.

Check failure on line 58 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Referencing prefixed PHPStan class: _PHPStan_c875e8309\Psr\Container\ContainerInterface.
{
parent::__construct($request, $response, $configuration);

if($locator !== NULL) {

Check failure on line 62 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 space(s) after IF keyword; 0 found

Check failure on line 62 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected newline after closing parenthesis; found 1 space(s)

Check failure on line 62 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

The opening brace of a multi-line IF statement must be on a new line
$this->jsonretriever = $locator->get('jsonretriever');

Check failure on line 63 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Call to method get() on an unknown class _PHPStan_c875e8309\Psr\Container\ContainerInterface.
$this->jsonresolver = $locator->get('jsonresolver');

Check warning on line 64 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

Check failure on line 64 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Call to method get() on an unknown class _PHPStan_c875e8309\Psr\Container\ContainerInterface.
$this->jsonvalidator = $locator->get('jsonvalidator');

Check failure on line 65 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Call to method get() on an unknown class _PHPStan_c875e8309\Psr\Container\ContainerInterface.

$this->configuration->load_file('validation');
$this->allowlist = $this->configuration['validation']['schemalist']->toArray();
}
}

/**
Expand Down Expand Up @@ -81,6 +118,11 @@ public function print_page()
header('Content-type: application/json');
http_response_code($code);

if(in_array($code, $this->configuration['validation']['http_codes']->toArray())) {

Check failure on line 121 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 space(s) after IF keyword; 0 found

Check failure on line 121 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected newline after closing parenthesis; found 1 space(s)

Check failure on line 121 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

The opening brace of a multi-line IF statement must be on a new line
$this->get_json_validated($json);
}

Check failure on line 124 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Functions must not contain multiple empty lines in a row; found 2 empty lines

if ($this->request->sapi == 'cli')
{
echo json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
Expand Down Expand Up @@ -162,6 +204,39 @@ public function print_exception($e)
}
}

Check failure on line 205 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 blank line after function; 2 found


/**
* Check if the JSON needs to be validated and do so if needed.
*
* @param array $json the response data passed by reference

Check failure on line 211 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Parameter comment must start with a capital letter
*
* @return void
*/
private function get_json_validated(array &$json): void
{

$schema_name = $this->request->controller . '/' . $this->request->method;
if(!array_key_exists($schema_name, $this->allowlist))

Check failure on line 219 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Expected 1 space(s) after IF keyword; 0 found
{
return;
}

$schema_path = $this->request->application_path . str_replace($this->request->base_path, '', $this->statics($this->allowlist[$schema_name]));
if(!is_readable($schema_path)) {
$json['status']['json_message'] = 'JSON schema was not found, not validated';
return;
}

$schema_file = $this->jsonretriever->retrieve('file://' . $schema_path);

Check failure on line 230 in src/Lunr/Corona/JsonView.php

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Call to method retrieve() on an unknown class JsonSchema\Uri\UriRetriever.
$this->jsonresolver->resolve($schema_file);
$this->jsonvalidator->check(json_decode(json_encode($json)), $schema_file);

if (!$this->jsonvalidator->isValid())
{
$json['status']['json_message'] = 'JSON failed to validate against the schema';
$json['status']['json_code'] = HttpCode::INTERNAL_SERVER_ERROR;
}
}
}

?>

0 comments on commit 942d7d9

Please sign in to comment.