Skip to content

Commit

Permalink
Update Schema to allow Numbers for 3.1 min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Nov 5, 2024
1 parent 9ae960c commit c6f456f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/spec/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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']), ['bool', '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']), ['bool', 'double', 'integer'])) {
throw new TypeErrorException(sprintf('Schema::$exclusiveMinimum MUST be either boolean or a number, "%s" given', gettype($data['exclusiveMinimum'])));
}

parent::__construct($data);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/spec/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c6f456f

Please sign in to comment.