From a2acabda872bf2876eccb55d953a55c01e81617e Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Thu, 8 Aug 2019 17:10:18 +0200 Subject: [PATCH] Update the JSON Schema Test Suite to version 2.0.0 to fetch test suites on Draft 06 and Draft 07. Updated validation on draft 04 since changes have been done on the test suites. --- composer.json | 7 +-- .../Constraints/ObjectConstraint.php | 5 ++ tests/Constraints/MinMaxPropertiesTest.php | 51 +++++++++++++++---- tests/Drafts/Draft4Test.php | 13 +++-- 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 8a2de30e..3fcd3772 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", "icecave/parity": "1.0.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1", - "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 dd1c02b9..45cffe23 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -180,6 +180,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) ); } }