From 52e3216dc1add8c2c39854f8ca7c8ced782beb65 Mon Sep 17 00:00:00 2001 From: John Charman Date: Mon, 18 Nov 2024 16:14:29 +0000 Subject: [PATCH] Add metadata keywords "title" and "description" --- src/Factory/V30/FromCebe.php | 2 ++ src/ValueObject/Partial/Schema.php | 6 ++++++ src/ValueObject/Valid/V30/Schema.php | 6 ++++++ tests/fixtures/Helper/PartialHelper.php | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/src/Factory/V30/FromCebe.php b/src/Factory/V30/FromCebe.php index f474d9b..b895bb1 100644 --- a/src/Factory/V30/FromCebe.php +++ b/src/Factory/V30/FromCebe.php @@ -175,6 +175,8 @@ enum: isset($schema->enum) ? self::createSchema($schema->additionalProperties) ?? true) : true, format: $schema->format ?? null, + title: $schema->title ?? null, + description: $schema->description ?? null, ); } diff --git a/src/ValueObject/Partial/Schema.php b/src/ValueObject/Partial/Schema.php index c26812c..47fb2c8 100644 --- a/src/ValueObject/Partial/Schema.php +++ b/src/ValueObject/Partial/Schema.php @@ -124,6 +124,12 @@ public function __construct( * https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-7 */ public string|null $format = null, + /** + * Keywords that provide additional metadata + * https://json-schema.org/draft/2020-12/json-schema-validation#section-9 + */ + public string|null $title = null, + public string|null $description = null, ) { } } diff --git a/src/ValueObject/Valid/V30/Schema.php b/src/ValueObject/Valid/V30/Schema.php index 09a05ba..8196e27 100644 --- a/src/ValueObject/Valid/V30/Schema.php +++ b/src/ValueObject/Valid/V30/Schema.php @@ -57,6 +57,9 @@ final class Schema extends Validated implements Valid\Schema public readonly string|null $format; + public readonly string|null $title; + public readonly string|null $description; + /** @var Type[] */ private readonly array $typesItCanBe; @@ -104,6 +107,9 @@ public function __construct( $this->format = $schema->format; + $this->title = $schema->title; + $this->description = $schema->description; + $this->typesItCanBe = array_map( fn($t) => Type::from($t), $this->typesItCanBe() diff --git a/tests/fixtures/Helper/PartialHelper.php b/tests/fixtures/Helper/PartialHelper.php index 40b3e63..cda0cba 100644 --- a/tests/fixtures/Helper/PartialHelper.php +++ b/tests/fixtures/Helper/PartialHelper.php @@ -160,6 +160,8 @@ public static function createSchema( array|null $oneOf = null, Schema|null $not = null, string|null $format = null, + string|null $title = null, + string|null $description = null, ): Schema { return new Schema( type: $type, @@ -187,6 +189,8 @@ enum: $enum, items: $items, properties: $properties, format: $format, + title: $title, + description: $description, ); } }