diff --git a/composer.json b/composer.json index c6b6936c..df1b806c 100644 --- a/composer.json +++ b/composer.json @@ -28,12 +28,13 @@ ], "require": { "php": ">=5.3.3", + "ext-json": "*", "marc-mabe/php-enum":"^2.0 || ^3.0 || ^4.0", "icecave/parity": "1.0.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "~2.2.20 || ~2.19.0", - "json-schema/json-schema-test-suite": "1.2.0", + "json-schema/json-schema-test-suite": "2.0.0", "phpunit/phpunit": "^4.8.35" }, "extra": { @@ -56,11 +57,11 @@ "type": "package", "package": { "name": "json-schema/json-schema-test-suite", - "version": "1.2.0", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/json-schema/JSON-Schema-Test-Suite", - "reference": "1.2.0" + "reference": "2.0.0" } } } diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index b4f85650..bd77c7ac 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -173,6 +173,11 @@ protected function &getProperty(&$element, $property, $fallback = null) */ protected function validateMinMaxConstraint($element, $objectDefinition, JsonPointer $path = null) { + // minProperties and maxProperties constraints only applies on objects elements. + if (!is_object($element)) { + return; + } + // Verify minimum number of properties if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) { if ($this->getTypeCheck()->propertyCount($element) < $objectDefinition->minProperties) { diff --git a/tests/Constraints/MinMaxPropertiesTest.php b/tests/Constraints/MinMaxPropertiesTest.php index 90774b91..8ce2647c 100644 --- a/tests/Constraints/MinMaxPropertiesTest.php +++ b/tests/Constraints/MinMaxPropertiesTest.php @@ -63,6 +63,39 @@ public function getValidTests() } }' ), + // Ignore array. + array( + '{ + "value": [] + }', + '{ + "properties": { + "value": {"minProperties": 1,"maxProperties": 2} + } + }' + ), + // Ignore string. + array( + '{ + "value": "foo" + }', + '{ + "properties": { + "value": {"minProperties": 1,"maxProperties": 2} + } + }' + ), + // Ignore anything that is non-object. + array( + '{ + "value": 42 + }', + '{ + "properties": { + "value": {"minProperties": 1,"maxProperties": 2} + } + }' + ), ); } @@ -123,16 +156,14 @@ public function getInvalidTests() } }' ), - array( - '{ - "value": [] - }', - '{ - "properties": { - "value": {"minProperties": 1,"maxProperties": 2} - } - }' - ), ); } + + /** + * {@inheritdoc} + */ + public function getInvalidForAssocTests() + { + return array(); + } } diff --git a/tests/Drafts/Draft4Test.php b/tests/Drafts/Draft4Test.php index 54eee4c4..73d1d5a9 100644 --- a/tests/Drafts/Draft4Test.php +++ b/tests/Drafts/Draft4Test.php @@ -33,7 +33,10 @@ public function getInvalidForAssocTests() $tests = parent::getInvalidForAssocTests(); unset( $tests['type.json / object type matches objects / an array is not an object'], - $tests['type.json / array type matches arrays / an object is not an array'] + $tests['type.json / array type matches arrays / an object is not an array'], + // Arrays must be ignored and in assoc case, these data are arrays and not objects. + $tests['maxProperties.json / maxProperties validation / too long is invalid'], + $tests['minProperties.json / minProperties validation / too short is invalid'] ); return $tests; @@ -44,7 +47,9 @@ public function getValidForAssocTests() $tests = parent::getValidForAssocTests(); unset( $tests['type.json / object type matches objects / an array is not an object'], - $tests['type.json / array type matches arrays / an object is not an array'] + $tests['type.json / array type matches arrays / an object is not an array'], + // Arrays must be ignored and in assoc case, these data are arrays and not objects. + $tests['required.json / required validation / ignores arrays'] ); return $tests; @@ -58,10 +63,12 @@ protected function getSkippedTests() return array( // Optional 'bignum.json', + 'ecmascript-regex.json', 'format.json', 'zeroTerminatedFloats.json', // Required - 'not.json' // only one test case failing + 'not.json', // only one test case failing + 'ref.json', // external references can not be found (localhost:1234) ); } }