Skip to content

Commit 81ca09b

Browse files
committed
Merge pull request #232 from rtucek/fix-issue-215
Moving constraints min- and maxProperties to ObjectConstraint
2 parents 5bf2286 + c2a8745 commit 81ca09b

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ public function validatePatternProperties($element, $path, $patternProperties)
7979
*/
8080
public function validateElement($element, $matches, $objectDefinition = null, $path = null, $additionalProp = null)
8181
{
82+
$this->validateMinMaxConstraint($element, $objectDefinition, $path);
8283
foreach ($element as $i => $value) {
8384

8485
$property = $this->getProperty($element, $i, new UndefinedConstraint());
8586
$definition = $this->getProperty($objectDefinition, $i);
8687

88+
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
89+
8790
// no additional properties allowed
8891
if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) {
8992
$this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties", 'additionalProp');
@@ -146,4 +149,26 @@ protected function getProperty($element, $property, $fallback = null)
146149

147150
return $fallback;
148151
}
152+
153+
/**
154+
* validating minimum and maximum property constraints (if present) against an element
155+
*
156+
* @param \stdClass $element Element to validate
157+
* @param \stdClass $objectDefinition ObjectConstraint definition
158+
* @param string $path Path to test?
159+
*/
160+
protected function validateMinMaxConstraint($element, $objectDefinition, $path) {
161+
// Verify minimum number of properties
162+
if (isset($objectDefinition->minProperties)) {
163+
if (count(get_object_vars($element)) < $objectDefinition->minProperties) {
164+
$this->addError($path, "Must contain a minimum of " . $objectDefinition->minProperties . " properties", 'minProperties', array('minProperties' => $objectDefinition->minProperties,));
165+
}
166+
}
167+
// Verify maximum number of properties
168+
if (isset($objectDefinition->maxProperties)) {
169+
if (count(get_object_vars($element)) > $objectDefinition->maxProperties) {
170+
$this->addError($path, "Must contain no more than " . $objectDefinition->maxProperties . " properties", 'maxProperties', array('maxProperties' => $objectDefinition->maxProperties,));
171+
}
172+
}
173+
}
149174
}

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,10 @@ class UndefinedConstraint extends Constraint
2525
*/
2626
public function check($value, $schema = null, $path = null, $i = null)
2727
{
28-
if (is_null($schema)) {
28+
if (is_null($schema) || !is_object($schema)) {
2929
return;
3030
}
3131

32-
if (!is_object($schema)) {
33-
throw new InvalidArgumentException(
34-
'Given schema must be an object in ' . $path
35-
. ' but is a ' . gettype($schema)
36-
);
37-
}
38-
3932
$i = is_null($i) ? "" : $i;
4033
$path = $this->incrementPath($path, $i);
4134

@@ -65,10 +58,10 @@ public function validateTypes($value, $schema = null, $path = null, $i = null)
6558
}
6659

6760
// check object
68-
if (is_object($value) && (isset($schema->properties) || isset($schema->patternProperties) || isset($schema->additionalProperties))) {
61+
if (is_object($value)) {
6962
$this->checkObject(
7063
$value,
71-
isset($schema->properties) ? $schema->properties : null,
64+
isset($schema->properties) ? $schema->properties : $schema,
7265
$path,
7366
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
7467
isset($schema->patternProperties) ? $schema->patternProperties : null
@@ -165,20 +158,6 @@ protected function validateCommonProperties($value, $schema = null, $path = null
165158
}
166159
}
167160

168-
// Verify minimum and maximum number of properties
169-
if (is_object($value)) {
170-
if (isset($schema->minProperties)) {
171-
if (count(get_object_vars($value)) < $schema->minProperties) {
172-
$this->addError($path, "Must contain a minimum of " . $schema->minProperties . " properties", 'minProperties', array('minProperties' => $schema->minProperties,));
173-
}
174-
}
175-
if (isset($schema->maxProperties)) {
176-
if (count(get_object_vars($value)) > $schema->maxProperties) {
177-
$this->addError($path, "Must contain no more than " . $schema->maxProperties . " properties", 'maxProperties', array('maxProperties' => $schema->maxProperties,));
178-
}
179-
}
180-
}
181-
182161
// Verify that dependencies are met
183162
if (is_object($value) && isset($schema->dependencies)) {
184163
$this->validateDependencies($value, $schema->dependencies, $path);

0 commit comments

Comments
 (0)