Skip to content
Snippets Groups Projects
Commit 72fd66b3 authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

fix: input validator json schema

parent 938154b9
Branches
Tags
No related merge requests found
...@@ -4,6 +4,8 @@ declare(strict_types=1); ...@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Distantmagic\Resonance; namespace Distantmagic\Resonance;
use LogicException;
use Opis\JsonSchema\Exceptions\ParseException;
use Swoole\Http\Request; use Swoole\Http\Request;
/** /**
...@@ -28,15 +30,28 @@ abstract readonly class InputValidator ...@@ -28,15 +30,28 @@ abstract readonly class InputValidator
$this->jsonSchema = $this->makeSchema(); $this->jsonSchema = $this->makeSchema();
} }
public function getSchema(): JsonSchema
{
return $this->jsonSchema;
}
/** /**
* @return InputValidationResult<TValidatedModel> * @return InputValidationResult<TValidatedModel>
*/ */
public function validateData(mixed $data): InputValidationResult public function validateData(mixed $data): InputValidationResult
{ {
/** try {
* @var JsonSchemaValidationResult<TValidatedData> /**
*/ * @var JsonSchemaValidationResult<TValidatedData>
$jsonSchemaValidationResult = $this->jsonSchemaValidator->validate($this->jsonSchema, $data); */
$jsonSchemaValidationResult = $this->jsonSchemaValidator->validate($this->jsonSchema, $data);
} catch (ParseException $parseException) {
throw new LogicException(sprintf(
'JSON schema is invalid in: "%s" "%s"',
$this::class,
), 0, $parseException);
}
$errors = $jsonSchemaValidationResult->errors; $errors = $jsonSchemaValidationResult->errors;
if (empty($errors)) { if (empty($errors)) {
......
...@@ -13,8 +13,8 @@ use Distantmagic\Resonance\StaticPageContentType; ...@@ -13,8 +13,8 @@ use Distantmagic\Resonance\StaticPageContentType;
use Generator; use Generator;
/** /**
* @extends InputValidator<FrontMatter, array{ * @extends InputValidator<FrontMatter, object{
* collections: array<string|array{ name: string, next: string }>, * collections: array<string|object{ name: string, next: string }>,
* content_type: string, * content_type: string,
* description: string, * description: string,
* draft: bool, * draft: bool,
...@@ -30,18 +30,18 @@ readonly class FrontMatterValidator extends InputValidator ...@@ -30,18 +30,18 @@ readonly class FrontMatterValidator extends InputValidator
{ {
protected function castValidatedData(mixed $data): FrontMatter protected function castValidatedData(mixed $data): FrontMatter
{ {
$collections = iterator_to_array($this->normalizeDataCollections($data['collections'])); $collections = iterator_to_array($this->normalizeDataCollections($data->collections));
return new FrontMatter( return new FrontMatter(
collections: $collections, collections: $collections,
contentType: StaticPageContentType::from($data['content_type']), contentType: StaticPageContentType::from($data->content_type),
description: trim($data['description']), description: trim($data->description),
isDraft: $data['draft'], isDraft: $data->draft,
layout: $data['layout'], layout: $data->layout,
next: $data['next'] ?? null, next: $data->next ?? null,
parent: $data['parent'] ?? null, parent: $data->parent ?? null,
registerStylesheets: $data['register_stylesheets'], registerStylesheets: $data->register_stylesheets,
title: trim($data['title']), title: trim($data->title),
); );
} }
...@@ -106,10 +106,8 @@ readonly class FrontMatterValidator extends InputValidator ...@@ -106,10 +106,8 @@ readonly class FrontMatterValidator extends InputValidator
'register_stylesheets' => [ 'register_stylesheets' => [
'type' => 'array', 'type' => 'array',
'items' => [ 'items' => [
[ 'type' => 'string',
'type' => 'string', 'minLength' => 1,
'minLength' => 1,
],
], ],
'default' => [], 'default' => [],
], ],
...@@ -134,8 +132,8 @@ readonly class FrontMatterValidator extends InputValidator ...@@ -134,8 +132,8 @@ readonly class FrontMatterValidator extends InputValidator
yield new FrontMatterCollectionReference($collection, null); yield new FrontMatterCollectionReference($collection, null);
} else { } else {
yield new FrontMatterCollectionReference( yield new FrontMatterCollectionReference(
$collection['name'], $collection->name,
$collection['next'], $collection->next,
); );
} }
} }
......
...@@ -46,16 +46,13 @@ readonly class RPCMessageValidator extends InputValidator ...@@ -46,16 +46,13 @@ readonly class RPCMessageValidator extends InputValidator
[ [
'type' => 'string', 'type' => 'string',
'enum' => $this->rpcMethodValidator->names(), 'enum' => $this->rpcMethodValidator->names(),
'required' => true,
], ],
[ [
'required' => true,
], ],
[ [
'type' => 'string', 'type' => 'string',
'format' => 'uuid', 'format' => 'uuid',
'nullable' => true, 'nullable' => true,
'required' => true,
], ],
], ],
]); ]);
......
...@@ -34,27 +34,22 @@ final readonly class StaticPageConfigurationProvider extends ConfigurationProvid ...@@ -34,27 +34,22 @@ final readonly class StaticPageConfigurationProvider extends ConfigurationProvid
'base_url' => [ 'base_url' => [
'type' => 'string', 'type' => 'string',
'minLength' => 1, 'minLength' => 1,
'required' => true,
], ],
'esbuild_metafile' => [ 'esbuild_metafile' => [
'type' => 'string', 'type' => 'string',
'minLength' => 1, 'minLength' => 1,
'required' => true,
], ],
'input_directory' => [ 'input_directory' => [
'type' => 'string', 'type' => 'string',
'minLength' => 1, 'minLength' => 1,
'required' => true,
], ],
'output_directory' => [ 'output_directory' => [
'type' => 'string', 'type' => 'string',
'minLength' => 1, 'minLength' => 1,
'required' => true,
], ],
'sitemap' => [ 'sitemap' => [
'type' => 'string', 'type' => 'string',
'minLength' => 1, 'minLength' => 1,
'required' => true,
], ],
], ],
'required' => ['base_url', 'esbuild_metafile', 'input_directory', 'output_directory', 'sitemap'], 'required' => ['base_url', 'esbuild_metafile', 'input_directory', 'output_directory', 'sitemap'],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment