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 5f3b7ea
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"php": ">=8.1",
"psr/cache": ">=1.0",
"lunr/config": "dev-master",
"lunr/cliparser": "dev-master"
"lunr/cliparser": "dev-master",
"justinrainbow/json-schema": "^6.0"
},
"suggest": {
"ext-http": "Needed for HTTP support in Corona, needed to get hostname in Shadow when parsing data",
Expand Down
8 changes: 8 additions & 0 deletions decomposer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@
"prefix": "Psr\\Cache",
"search-path": "/src/"
}
},
"JsonSchema": {
"url": "https://github.com/jsonrainbow/json-schema.git",
"version": "6.0.0",
"psr4": {
"prefix": "JsonSchema",
"search-path": "/src/JsonSchema"
}
}
}
73 changes: 65 additions & 8 deletions src/Lunr/Corona/JsonView.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Lunr\Corona;

use JsonSchema\Validator;
use Lunr\Core\Configuration;
use stdClass;
use Throwable;
Expand All @@ -20,16 +21,36 @@
class JsonView extends View
{

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

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

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

Class Lunr\Corona\JsonView has an uninitialized readonly property $json_validator. Assign it in the constructor.

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

/**

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

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Doc comment for parameter "$validator" missing
* 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 Validator|null $locator Shared instance of the Validator class

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

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Doc comment for parameter $locator does not match actual variable name $validator
*/
public function __construct($request, $response, $configuration)
public function __construct($request, $response, $configuration, $validator = NULL)

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

View workflow job for this annotation

GitHub Actions / php-tests / phpstan / PHPStan

PHPDoc tag @param references unknown parameter: $locator
{
parent::__construct($request, $response, $configuration);

if($validator !== NULL) {

Check failure on line 48 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 48 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 48 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->json_validator = $validator;

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

/**
Expand All @@ -47,7 +68,7 @@ public function __destruct()
*
* @return mixed $return Prepared response data
*/
protected function prepare_data($data)
protected function prepare_data($data): mixed
{
return $data;
}
Expand All @@ -57,7 +78,7 @@ protected function prepare_data($data)
*
* @return void
*/
public function print_page()
public function print_page(): void
{
$identifier = $this->response->get_return_code_identifiers(TRUE);

Expand All @@ -81,6 +102,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 105 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 105 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 105 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 108 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 All @@ -96,7 +122,7 @@ public function print_page()
*
* @return void
*/
public function print_fatal_error()
public function print_fatal_error(): void
{
$error = error_get_last();

Expand Down Expand Up @@ -133,7 +159,7 @@ public function print_fatal_error()
*
* @return void
*/
public function print_exception($e)
public function print_exception($e): void
{
$json = [];

Expand Down Expand Up @@ -162,6 +188,37 @@ public function print_exception($e)
}
}

Check failure on line 189 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
*
* @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))
{
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;
}

$this->json_validator->validate($data, (object)[ '$ref' => 'file://' . realpath($schema_path)]);

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

?>

0 comments on commit 5f3b7ea

Please sign in to comment.