Skip to content

Commit dd73ab2

Browse files
committed
Merge pull request #261 from schrobak/bugfix/conditional-min-max-properties-validation
Fixes min/max properties validation for non objects
2 parents 9ef71fd + 84decc9 commit dd73ab2

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,8 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
8181
{
8282
$this->validateMinMaxConstraint($element, $objectDefinition, $path);
8383
foreach ($element as $i => $value) {
84-
85-
$property = $this->getProperty($element, $i, new UndefinedConstraint());
8684
$definition = $this->getProperty($objectDefinition, $i);
8785

88-
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
89-
9086
// no additional properties allowed
9187
if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) {
9288
$this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties", 'additionalProp');
@@ -111,6 +107,11 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
111107
// normal property verification
112108
$this->checkUndefined($value, new \stdClass(), $path, $i);
113109
}
110+
111+
$property = $this->getProperty($element, $i, new UndefinedConstraint());
112+
if (is_object($property)) {
113+
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
114+
}
114115
}
115116
}
116117

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Tests\Constraints;
11+
12+
class MinMaxPropertiesTest extends BaseTestCase
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function getValidTests()
18+
{
19+
return array(
20+
array(
21+
'{
22+
"value": {}
23+
}',
24+
'{
25+
"type": "object",
26+
"properties": {
27+
"value": {"type": "object", "minProperties": 0}
28+
}
29+
}'
30+
),
31+
array(
32+
'{
33+
"value": {}
34+
}',
35+
'{
36+
"type": "object",
37+
"properties": {
38+
"value": {"type": "object", "maxProperties": 1}
39+
}
40+
}'
41+
),
42+
array(
43+
'{
44+
"value": {}
45+
}',
46+
'{
47+
"type": "object",
48+
"properties": {
49+
"value": {"type": "object", "minProperties": 0,"maxProperties": 1}
50+
}
51+
}'
52+
),
53+
array(
54+
'{
55+
"value": {"foo": 1, "bar": 2}
56+
}',
57+
'{
58+
"type": "object",
59+
"properties": {
60+
"value": {"type": "object", "minProperties": 1,"maxProperties": 2}
61+
}
62+
}'
63+
),
64+
);
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function getInvalidTests()
71+
{
72+
return array(
73+
array(
74+
'{
75+
"value": 1
76+
}',
77+
'{
78+
"type": "object",
79+
"properties": {
80+
"value": {"type": "object", "minProperties": 1}
81+
}
82+
}'
83+
),
84+
array(
85+
'{
86+
"value": 1
87+
}',
88+
'{
89+
"type": "object",
90+
"properties": {
91+
"value": {"type": "object", "maxProperties": 1}
92+
}
93+
}'
94+
),
95+
array(
96+
'{
97+
"value": {"foo": 1, "bar": 2, "baz": 3}
98+
}',
99+
'{
100+
"type": "object",
101+
"properties": {
102+
"value": {"type": "object", "minProperties": 1,"maxProperties": 2}
103+
}
104+
}'
105+
),
106+
);
107+
}
108+
}

0 commit comments

Comments
 (0)