Skip to content

Commit eccd850

Browse files
committed
1 parent dff04e3 commit eccd850

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,19 @@ protected function &getProperty(&$element, $property, $fallback = null)
175175
*/
176176
protected function validateMinMaxConstraint($element, $objectDefinition, ?JsonPointer $path = null)
177177
{
178+
if (!$this->getTypeCheck()::isObject($element)) {
179+
return;
180+
}
181+
178182
// Verify minimum number of properties
179-
if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) {
180-
if ($this->getTypeCheck()->propertyCount($element) < $objectDefinition->minProperties) {
183+
if (isset($objectDefinition->minProperties) && is_int($objectDefinition->minProperties)) {
184+
if ($this->getTypeCheck()->propertyCount($element) < max(0, $objectDefinition->minProperties)) {
181185
$this->addError(ConstraintError::PROPERTIES_MIN(), $path, ['minProperties' => $objectDefinition->minProperties]);
182186
}
183187
}
184188
// Verify maximum number of properties
185-
if (isset($objectDefinition->maxProperties) && !is_object($objectDefinition->maxProperties)) {
186-
if ($this->getTypeCheck()->propertyCount($element) > $objectDefinition->maxProperties) {
189+
if (isset($objectDefinition->maxProperties) && is_int($objectDefinition->maxProperties)) {
190+
if ($this->getTypeCheck()->propertyCount($element) > max(0, $objectDefinition->maxProperties)) {
187191
$this->addError(ConstraintError::PROPERTIES_MAX(), $path, ['maxProperties' => $objectDefinition->maxProperties]);
188192
}
189193
}

tests/Constraints/MinMaxPropertiesTest.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MinMaxPropertiesTest extends BaseTestCase
1919
public function getValidTests(): array
2020
{
2121
return [
22-
[
22+
'Empty object with minProperties: 0' => [
2323
'{
2424
"value": {}
2525
}',
@@ -30,7 +30,7 @@ public function getValidTests(): array
3030
}
3131
}'
3232
],
33-
[
33+
'Empty object with maxProperties: 1' => [
3434
'{
3535
"value": {}
3636
}',
@@ -41,7 +41,7 @@ public function getValidTests(): array
4141
}
4242
}'
4343
],
44-
[
44+
'Empty object with minProperties: 0 and maxProperties: 1' => [
4545
'{
4646
"value": {}
4747
}',
@@ -52,7 +52,7 @@ public function getValidTests(): array
5252
}
5353
}'
5454
],
55-
[
55+
'Object with two properties with minProperties: 1 and maxProperties: 2' => [
5656
'{
5757
"value": {"foo": 1, "bar": 2}
5858
}',
@@ -63,6 +63,26 @@ public function getValidTests(): array
6363
}
6464
}'
6565
],
66+
'Empty array with minProperties: 1 and maxProperties: 2' => [
67+
'{
68+
"value": []
69+
}',
70+
'{
71+
"properties": {
72+
"value": {"minProperties": 1,"maxProperties": 2}
73+
}
74+
}'
75+
],
76+
'Array with two items with maxProperties: 1' => [
77+
'{
78+
"value": [1, 2]
79+
}',
80+
'{
81+
"properties": {
82+
"value": {""maxProperties": 1}
83+
}
84+
}'
85+
],
6686
];
6787
}
6888

@@ -72,7 +92,7 @@ public function getValidTests(): array
7292
public function getInvalidTests(): array
7393
{
7494
return [
75-
[
95+
'Empty object with minProperties: 1' => [
7696
'{
7797
"value": {}
7898
}',
@@ -83,7 +103,7 @@ public function getInvalidTests(): array
83103
}
84104
}'
85105
],
86-
[
106+
'Empty object with minProperties' => [
87107
'{}',
88108
'{
89109
"type": "object",
@@ -98,7 +118,7 @@ public function getInvalidTests(): array
98118
"minProperties": 1
99119
}'
100120
],
101-
[
121+
'Object with two properties with maxProperties: 1' => [
102122
'{
103123
"value": {
104124
"propertyOne": "valueOne",
@@ -112,7 +132,7 @@ public function getInvalidTests(): array
112132
}
113133
}'
114134
],
115-
[
135+
'Object with two properties with minProperties: 1 and maxProperties: 2' => [
116136
'{
117137
"value": {"foo": 1, "bar": 2, "baz": 3}
118138
}',
@@ -123,16 +143,6 @@ public function getInvalidTests(): array
123143
}
124144
}'
125145
],
126-
[
127-
'{
128-
"value": []
129-
}',
130-
'{
131-
"properties": {
132-
"value": {"minProperties": 1,"maxProperties": 2}
133-
}
134-
}'
135-
],
136146
];
137147
}
138148
}

0 commit comments

Comments
 (0)