diff --git a/src/InputValidator.php b/src/InputValidator.php index 5ce18e3e0829a9ba14307239c89425721ee9ac56..0fa3e56b5f2605c4278a73de19f743b67861620f 100644 --- a/src/InputValidator.php +++ b/src/InputValidator.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Distantmagic\Resonance; +use LogicException; +use Opis\JsonSchema\Exceptions\ParseException; use Swoole\Http\Request; /** @@ -28,15 +30,28 @@ abstract readonly class InputValidator $this->jsonSchema = $this->makeSchema(); } + public function getSchema(): JsonSchema + { + return $this->jsonSchema; + } + /** * @return InputValidationResult<TValidatedModel> */ public function validateData(mixed $data): InputValidationResult { - /** - * @var JsonSchemaValidationResult<TValidatedData> - */ - $jsonSchemaValidationResult = $this->jsonSchemaValidator->validate($this->jsonSchema, $data); + try { + /** + * @var JsonSchemaValidationResult<TValidatedData> + */ + $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; if (empty($errors)) { diff --git a/src/InputValidator/FrontMatterValidator.php b/src/InputValidator/FrontMatterValidator.php index 665f6443844e088ae92dd22573109039f3c1cda9..394cc3ba1b3b5155fbd65e26d5df49071f3d8131 100644 --- a/src/InputValidator/FrontMatterValidator.php +++ b/src/InputValidator/FrontMatterValidator.php @@ -13,8 +13,8 @@ use Distantmagic\Resonance\StaticPageContentType; use Generator; /** - * @extends InputValidator<FrontMatter, array{ - * collections: array<string|array{ name: string, next: string }>, + * @extends InputValidator<FrontMatter, object{ + * collections: array<string|object{ name: string, next: string }>, * content_type: string, * description: string, * draft: bool, @@ -30,18 +30,18 @@ readonly class FrontMatterValidator extends InputValidator { 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( collections: $collections, - contentType: StaticPageContentType::from($data['content_type']), - description: trim($data['description']), - isDraft: $data['draft'], - layout: $data['layout'], - next: $data['next'] ?? null, - parent: $data['parent'] ?? null, - registerStylesheets: $data['register_stylesheets'], - title: trim($data['title']), + contentType: StaticPageContentType::from($data->content_type), + description: trim($data->description), + isDraft: $data->draft, + layout: $data->layout, + next: $data->next ?? null, + parent: $data->parent ?? null, + registerStylesheets: $data->register_stylesheets, + title: trim($data->title), ); } @@ -106,10 +106,8 @@ readonly class FrontMatterValidator extends InputValidator 'register_stylesheets' => [ 'type' => 'array', 'items' => [ - [ - 'type' => 'string', - 'minLength' => 1, - ], + 'type' => 'string', + 'minLength' => 1, ], 'default' => [], ], @@ -134,8 +132,8 @@ readonly class FrontMatterValidator extends InputValidator yield new FrontMatterCollectionReference($collection, null); } else { yield new FrontMatterCollectionReference( - $collection['name'], - $collection['next'], + $collection->name, + $collection->next, ); } } diff --git a/src/InputValidator/RPCMessageValidator.php b/src/InputValidator/RPCMessageValidator.php index fa595bc64870a2f7bc944be7e95e5389a3edb708..3acf1f71c576a53ce51fd848e9e4d14fed086920 100644 --- a/src/InputValidator/RPCMessageValidator.php +++ b/src/InputValidator/RPCMessageValidator.php @@ -46,16 +46,13 @@ readonly class RPCMessageValidator extends InputValidator [ 'type' => 'string', 'enum' => $this->rpcMethodValidator->names(), - 'required' => true, ], [ - 'required' => true, ], [ 'type' => 'string', 'format' => 'uuid', 'nullable' => true, - 'required' => true, ], ], ]); diff --git a/src/SingletonProvider/ConfigurationProvider/StaticPageConfigurationProvider.php b/src/SingletonProvider/ConfigurationProvider/StaticPageConfigurationProvider.php index b5f87b6943e2d9e8936972fde8414a0f4847290c..02c121c54d2daec13d0902807bbbfec58f95c7fc 100644 --- a/src/SingletonProvider/ConfigurationProvider/StaticPageConfigurationProvider.php +++ b/src/SingletonProvider/ConfigurationProvider/StaticPageConfigurationProvider.php @@ -34,27 +34,22 @@ final readonly class StaticPageConfigurationProvider extends ConfigurationProvid 'base_url' => [ 'type' => 'string', 'minLength' => 1, - 'required' => true, ], 'esbuild_metafile' => [ 'type' => 'string', 'minLength' => 1, - 'required' => true, ], 'input_directory' => [ 'type' => 'string', 'minLength' => 1, - 'required' => true, ], 'output_directory' => [ 'type' => 'string', 'minLength' => 1, - 'required' => true, ], 'sitemap' => [ 'type' => 'string', 'minLength' => 1, - 'required' => true, ], ], 'required' => ['base_url', 'esbuild_metafile', 'input_directory', 'output_directory', 'sitemap'],