Skip to content

Commit

Permalink
fix: input validator json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Jan 13, 2024
1 parent 938154b commit 72fd66b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
23 changes: 19 additions & 4 deletions src/InputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Distantmagic\Resonance;

use LogicException;
use Opis\JsonSchema\Exceptions\ParseException;
use Swoole\Http\Request;

/**
Expand All @@ -28,15 +30,28 @@ public function __construct(private JsonSchemaValidator $jsonSchemaValidator)
$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(

Check failure on line 49 in src/InputValidator.php

View workflow job for this annotation

GitHub Actions / psalm

TooFewArguments

src/InputValidator.php:49:38: TooFewArguments: Too few arguments for sprintf (see https://psalm.dev/025)
'JSON schema is invalid in: "%s" "%s"',
$this::class,
), 0, $parseException);
}

$errors = $jsonSchemaValidationResult->errors;

if (empty($errors)) {
Expand Down
32 changes: 15 additions & 17 deletions src/InputValidator/FrontMatterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
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,
Expand All @@ -30,18 +30,18 @@
{
protected function castValidatedData(mixed $data): FrontMatter
{
$collections = iterator_to_array($this->normalizeDataCollections($data['collections']));
$collections = iterator_to_array($this->normalizeDataCollections($data->collections));

Check failure on line 33 in src/InputValidator/FrontMatterValidator.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidArgument

src/InputValidator/FrontMatterValidator.php:33:74: InvalidArgument: Argument 1 of Distantmagic\Resonance\InputValidator\FrontMatterValidator::normalizeDataCollections expects array<array-key, array{name: string, next: string}|string>, but array<array-key, object{name:string, next:string}|string> provided (see https://psalm.dev/004)

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),
);
}

Expand Down Expand Up @@ -106,10 +106,8 @@ protected function makeSchema(): JsonSchema
'register_stylesheets' => [
'type' => 'array',
'items' => [
[
'type' => 'string',
'minLength' => 1,
],
'type' => 'string',
'minLength' => 1,
],
'default' => [],
],
Expand All @@ -134,8 +132,8 @@ private function normalizeDataCollections(array $collections): Generator
yield new FrontMatterCollectionReference($collection, null);
} else {
yield new FrontMatterCollectionReference(
$collection['name'],
$collection['next'],
$collection->name,

Check failure on line 135 in src/InputValidator/FrontMatterValidator.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidPropertyFetch

src/InputValidator/FrontMatterValidator.php:135:21: InvalidPropertyFetch: Cannot fetch property on non-object $collection of type array{name: string, next: string} (see https://psalm.dev/029)

Check failure on line 135 in src/InputValidator/FrontMatterValidator.php

View workflow job for this annotation

GitHub Actions / psalm

MixedArgument

src/InputValidator/FrontMatterValidator.php:135:21: MixedArgument: Argument 1 of Distantmagic\Resonance\FrontMatterCollectionReference::__construct cannot be mixed, expecting string (see https://psalm.dev/030)
$collection->next,

Check failure on line 136 in src/InputValidator/FrontMatterValidator.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidPropertyFetch

src/InputValidator/FrontMatterValidator.php:136:21: InvalidPropertyFetch: Cannot fetch property on non-object $collection of type array{name: string, next: string} (see https://psalm.dev/029)

Check failure on line 136 in src/InputValidator/FrontMatterValidator.php

View workflow job for this annotation

GitHub Actions / psalm

MixedArgument

src/InputValidator/FrontMatterValidator.php:136:21: MixedArgument: Argument 2 of Distantmagic\Resonance\FrontMatterCollectionReference::__construct cannot be mixed, expecting null|string (see https://psalm.dev/030)
);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/InputValidator/RPCMessageValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ protected function makeSchema(): JsonSchema
[
'type' => 'string',
'enum' => $this->rpcMethodValidator->names(),
'required' => true,
],
[
'required' => true,
],
[
'type' => 'string',
'format' => 'uuid',
'nullable' => true,
'required' => true,
],
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,22 @@ protected function makeSchema(): JsonSchema
'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'],
Expand Down

0 comments on commit 72fd66b

Please sign in to comment.