Skip to content

Commit

Permalink
Merge pull request #261 from schrobak/bugfix/conditional-min-max-prop…
Browse files Browse the repository at this point in the history
…erties-validation

Fixes min/max properties validation for non objects
  • Loading branch information
bighappyface committed May 9, 2016
2 parents 9ef71fd + 84decc9 commit dd73ab2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
{
$this->validateMinMaxConstraint($element, $objectDefinition, $path);
foreach ($element as $i => $value) {

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

$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);

// no additional properties allowed
if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) {
$this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties", 'additionalProp');
Expand All @@ -111,6 +107,11 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
// normal property verification
$this->checkUndefined($value, new \stdClass(), $path, $i);
}

$property = $this->getProperty($element, $i, new UndefinedConstraint());
if (is_object($property)) {
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
}
}
}

Expand Down
108 changes: 108 additions & 0 deletions tests/JsonSchema/Tests/Constraints/MinMaxPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JsonSchema\Tests\Constraints;

class MinMaxPropertiesTest extends BaseTestCase
{
/**
* {@inheritdoc}
*/
public function getValidTests()
{
return array(
array(
'{
"value": {}
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "minProperties": 0}
}
}'
),
array(
'{
"value": {}
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "maxProperties": 1}
}
}'
),
array(
'{
"value": {}
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "minProperties": 0,"maxProperties": 1}
}
}'
),
array(
'{
"value": {"foo": 1, "bar": 2}
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "minProperties": 1,"maxProperties": 2}
}
}'
),
);
}

/**
* {@inheritdoc}
*/
public function getInvalidTests()
{
return array(
array(
'{
"value": 1
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "minProperties": 1}
}
}'
),
array(
'{
"value": 1
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "maxProperties": 1}
}
}'
),
array(
'{
"value": {"foo": 1, "bar": 2, "baz": 3}
}',
'{
"type": "object",
"properties": {
"value": {"type": "object", "minProperties": 1,"maxProperties": 2}
}
}'
),
);
}
}

0 comments on commit dd73ab2

Please sign in to comment.