From 9e20902ca9ff160015e0958d6c92b517bb30f2f7 Mon Sep 17 00:00:00 2001 From: John Charman Date: Thu, 31 Oct 2024 18:31:36 +0000 Subject: [PATCH] Update Schema to allow Numbers for 3.1 min and max --- src/spec/Schema.php | 17 +++++++++++++---- tests/spec/SchemaTest.php | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/spec/Schema.php b/src/spec/Schema.php index 864a90e..49921d3 100644 --- a/src/spec/Schema.php +++ b/src/spec/Schema.php @@ -26,9 +26,9 @@ * @property string $title * @property int|float $multipleOf * @property int|float $maximum - * @property bool $exclusiveMaximum + * @property bool|int|float $exclusiveMaximum * @property int|float $minimum - * @property bool $exclusiveMinimum + * @property bool|int|float $exclusiveMinimum * @property int $maxLength * @property int $minLength * @property string $pattern (This string SHOULD be a valid regular expression, according to the [ECMA 262 regular expression dialect](https://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5)) @@ -75,9 +75,9 @@ protected function attributes(): array 'title' => Type::STRING, 'multipleOf' => Type::NUMBER, 'maximum' => Type::NUMBER, - 'exclusiveMaximum' => Type::BOOLEAN, + // 'exclusiveMaximum' => 'boolean' for 3.0 or 'number' for 3.1, handled in constructor, 'minimum' => Type::NUMBER, - 'exclusiveMinimum' => Type::BOOLEAN, + // 'exclusiveMinimum' => 'boolean' for 3.0 or 'number' for 3.1, handled in constructor, 'maxLength' => Type::INTEGER, 'minLength' => Type::INTEGER, 'pattern' => Type::STRING, @@ -151,6 +151,15 @@ public function __construct(array $data) throw new TypeErrorException(sprintf('Schema::$additionalProperties MUST be either boolean or a Schema/Reference object, "%s" given', $givenType)); } } + + if (isset($data['exclusiveMaximum']) && !in_array(gettype($data['exclusiveMaximum']), ['boolean', 'double', 'integer'])) { + throw new TypeErrorException(sprintf('Schema::$exclusiveMinimum MUST be either boolean or a number, "%s" given', gettype($data['exclusiveMaximum']))); + } + + if (isset($data['exclusiveMinimum']) && !in_array(gettype($data['exclusiveMinimum']), ['boolean', 'double', 'integer'])) { + throw new TypeErrorException(sprintf('Schema::$exclusiveMinimum MUST be either boolean or a number, "%s" given', gettype($data['exclusiveMinimum']))); + } + parent::__construct($data); } diff --git a/tests/spec/SchemaTest.php b/tests/spec/SchemaTest.php index 2cad357..2701694 100644 --- a/tests/spec/SchemaTest.php +++ b/tests/spec/SchemaTest.php @@ -102,6 +102,20 @@ public function testMinMax() $this->assertTrue($schema->exclusiveMaximum); $this->assertNull($schema->minimum); $this->assertNull($schema->exclusiveMinimum); + + /** @var $schema Schema */ + $schema = Reader::readFromJson('{"type": "integer", "exclusiveMaximum": 10}', Schema::class); + $this->assertNull($schema->maximum); + $this->assertSame(10, $schema->exclusiveMaximum); + $this->assertNull($schema->minimum); + $this->assertNull($schema->exclusiveMinimum); + + /** @var $schema Schema */ + $schema = Reader::readFromJson('{"type": "integer", "exclusiveMinimum": 10}', Schema::class); + $this->assertNull($schema->maximum); + $this->assertNull($schema->exclusiveMaximum); + $this->assertNull($schema->minimum); + $this->assertSame(10, $schema->exclusiveMinimum); } public function testReadObject()