Skip to content

Commit 9e20902

Browse files
charjrDEVizzent
authored andcommitted
Update Schema to allow Numbers for 3.1 min and max
1 parent 7ac86b4 commit 9e20902

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/spec/Schema.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
* @property string $title
2727
* @property int|float $multipleOf
2828
* @property int|float $maximum
29-
* @property bool $exclusiveMaximum
29+
* @property bool|int|float $exclusiveMaximum
3030
* @property int|float $minimum
31-
* @property bool $exclusiveMinimum
31+
* @property bool|int|float $exclusiveMinimum
3232
* @property int $maxLength
3333
* @property int $minLength
3434
* @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
7575
'title' => Type::STRING,
7676
'multipleOf' => Type::NUMBER,
7777
'maximum' => Type::NUMBER,
78-
'exclusiveMaximum' => Type::BOOLEAN,
78+
// 'exclusiveMaximum' => 'boolean' for 3.0 or 'number' for 3.1, handled in constructor,
7979
'minimum' => Type::NUMBER,
80-
'exclusiveMinimum' => Type::BOOLEAN,
80+
// 'exclusiveMinimum' => 'boolean' for 3.0 or 'number' for 3.1, handled in constructor,
8181
'maxLength' => Type::INTEGER,
8282
'minLength' => Type::INTEGER,
8383
'pattern' => Type::STRING,
@@ -151,6 +151,15 @@ public function __construct(array $data)
151151
throw new TypeErrorException(sprintf('Schema::$additionalProperties MUST be either boolean or a Schema/Reference object, "%s" given', $givenType));
152152
}
153153
}
154+
155+
if (isset($data['exclusiveMaximum']) && !in_array(gettype($data['exclusiveMaximum']), ['boolean', 'double', 'integer'])) {
156+
throw new TypeErrorException(sprintf('Schema::$exclusiveMinimum MUST be either boolean or a number, "%s" given', gettype($data['exclusiveMaximum'])));
157+
}
158+
159+
if (isset($data['exclusiveMinimum']) && !in_array(gettype($data['exclusiveMinimum']), ['boolean', 'double', 'integer'])) {
160+
throw new TypeErrorException(sprintf('Schema::$exclusiveMinimum MUST be either boolean or a number, "%s" given', gettype($data['exclusiveMinimum'])));
161+
}
162+
154163
parent::__construct($data);
155164
}
156165

tests/spec/SchemaTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ public function testMinMax()
102102
$this->assertTrue($schema->exclusiveMaximum);
103103
$this->assertNull($schema->minimum);
104104
$this->assertNull($schema->exclusiveMinimum);
105+
106+
/** @var $schema Schema */
107+
$schema = Reader::readFromJson('{"type": "integer", "exclusiveMaximum": 10}', Schema::class);
108+
$this->assertNull($schema->maximum);
109+
$this->assertSame(10, $schema->exclusiveMaximum);
110+
$this->assertNull($schema->minimum);
111+
$this->assertNull($schema->exclusiveMinimum);
112+
113+
/** @var $schema Schema */
114+
$schema = Reader::readFromJson('{"type": "integer", "exclusiveMinimum": 10}', Schema::class);
115+
$this->assertNull($schema->maximum);
116+
$this->assertNull($schema->exclusiveMaximum);
117+
$this->assertNull($schema->minimum);
118+
$this->assertSame(10, $schema->exclusiveMinimum);
105119
}
106120

107121
public function testReadObject()

0 commit comments

Comments
 (0)