Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes min/max properties validation for non objects #261

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
}
}'
),
);
}
}