Skip to content

Commit

Permalink
fix: JsonSchemaInterface implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Lelaisant committed Oct 26, 2022
1 parent 0b16fc1 commit 61c0b00
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/CollectionSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
/**
* @template I
*
* @phpstan-type CollectionSchemaData array<I>
* @type CollectionSchemaData array<I>
*
* @extends JsonSchema<CollectionSchemaData>
* @implements JsonSchemaInterface<CollectionSchemaData>
*/
abstract class CollectionSchema implements JsonSchemaInterface
{
/**
* @param JsonSchema<I> $itemSchema
* @psalm-param JsonSchemaInterface<I> $itemSchema
*/
public function __construct(private JsonSchema $itemSchema)
public function __construct(private JsonSchemaInterface $itemSchema)
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/EnumSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

/**
* @template E
* @extends JsonSchema<E>
*
* @implements JsonSchemaInterface<E>
*/
abstract class EnumSchema implements JsonSchemaInterface
{
Expand Down
22 changes: 12 additions & 10 deletions src/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Knp\JsonSchema;

use Knp\JsonSchema\Validator\Errors;

/**
* @template T of mixed
*
Expand Down Expand Up @@ -116,7 +118,7 @@ public function jsonSerialize(): array
*
* @return array<string, mixed>
*/
protected static function constant($value): array
public static function constant($value): array
{
return [
'const' => $value,
Expand All @@ -126,7 +128,7 @@ protected static function constant($value): array
/**
* @return array<string, mixed>
*/
protected static function null(): array
public static function null(): array
{
return [
'type' => 'null',
Expand All @@ -136,7 +138,7 @@ protected static function null(): array
/**
* @return array<string, mixed>
*/
protected static function text(): array
public static function text(): array
{
return [
'type' => 'string',
Expand All @@ -147,7 +149,7 @@ protected static function text(): array
/**
* @return array<string, mixed>
*/
protected static function boolean(): array
public static function boolean(): array
{
return [
'type' => 'boolean',
Expand All @@ -157,7 +159,7 @@ protected static function boolean(): array
/**
* @return array<string, mixed>
*/
protected static function string(?string $format = null): array
public static function string(?string $format = null): array
{
$result = [
...self::text(),
Expand All @@ -174,7 +176,7 @@ protected static function string(?string $format = null): array
/**
* @return array<string, mixed>
*/
protected static function integer(): array
public static function integer(): array
{
return [
'type' => 'integer',
Expand All @@ -184,7 +186,7 @@ protected static function integer(): array
/**
* @return array<string, mixed>
*/
protected static function number(): array
public static function number(): array
{
return [
'type' => 'number',
Expand All @@ -194,7 +196,7 @@ protected static function number(): array
/**
* @return array<string, mixed>
*/
protected static function date(): array
public static function date(): array
{
return [
'type' => 'string',
Expand All @@ -205,7 +207,7 @@ protected static function date(): array
/**
* @return array<string, mixed>
*/
protected static function positiveInteger(): array
public static function positiveInteger(): array
{
return [
...self::integer(),
Expand All @@ -218,7 +220,7 @@ protected static function positiveInteger(): array
*
* @return array{oneOf: array<array<string, mixed>>}
*/
protected static function oneOf(...$schemas): array
public static function oneOf(...$schemas): array
{
return [
'oneOf' => $schemas,
Expand Down
3 changes: 2 additions & 1 deletion src/JsonSchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Knp\JsonSchema;

use JsonSerializable;
use Knp\JsonSchema\Validator\Errors;

/**
* @template T of mixed
* @template T
*/
interface JsonSchemaInterface extends JsonSerializable
{
Expand Down
13 changes: 7 additions & 6 deletions src/ObjectSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Knp\JsonSchema;

/**
* @template T of array<string, mixed>
* @extends JsonSchema<T>
* @template T
*
* @implements JsonSchemaInterface<T>
*/
abstract class ObjectSchema implements JsonSchemaInterface
{
/**
* @var array<string, JsonSchema<mixed>>
* @var array<string, JsonSchemaInterface<mixed>>
*/
private array $properties = [];

Expand Down Expand Up @@ -44,11 +45,11 @@ protected function hasAdditionalProperties(): bool
}

/**
* @template S
* @psalm-template S
*
* @param JsonSchema<S> $schema
* @psalm-param JsonSchemaInterface<S> $schema
*/
protected function addProperty(string $name, JsonSchema $schema, bool $required = true): void
protected function addProperty(string $name, JsonSchemaInterface $schema, bool $required = true): void
{
$this->properties[$name] = $schema;

Expand Down
27 changes: 24 additions & 3 deletions src/Scalar/UuidSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace Knp\JsonSchema\Scalar;

use Knp\JsonSchema\JsonSchema;
use Knp\JsonSchema\JsonSchemaInterface;

/**
* @extends JsonSchema<string>
* @implements JsonSchemaInterface<string>
*/
final class UuidSchema extends JsonSchema
final class UuidSchema implements JsonSchemaInterface
{
private const PATTERN = '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$';

Expand Down Expand Up @@ -37,12 +38,32 @@ public function getDescription(): string
public function getSchema(): array
{
return array_merge(
self::string(),
JsonSchema::string(),
[
'pattern' => self::PATTERN,
'minLength' => 36,
'maxLength' => 36,
]
);
}

/**
* {@inheritdoc}
*/
public function jsonSerialize(): array
{
$schema = $this->getSchema();

/**
* @var array<string, mixed>&array{title: string, description: string, examples: array<T>}
*/
return array_merge(
$schema,
[
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'examples' => [...$this->getExamples()],
],
);
}
}
8 changes: 4 additions & 4 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
interface Validator
{
/**
* @template T
* @template T of array<array-key, mixed>
*
* @param T $data
* @param class-string<JsonSchema<T>>|JsonSchema<T> $schema
* @param array<array-key, mixed> $data
* @param JsonSchemaInterface<T> $schema
*/
public function validate($data, $schema): ?Errors;
public function validate(array $data, JsonSchemaInterface $schema): ?Errors;
}

0 comments on commit 61c0b00

Please sign in to comment.