From a2beb4e5877748ec8dc98265e763d3ef5dbdaa0f Mon Sep 17 00:00:00 2001 From: Paulo Ribeiro Date: Thu, 22 Oct 2015 09:15:32 -0200 Subject: [PATCH 1/3] bugfix: patternProperties raised errors when the pattern contained slash. Now the delimiter to evaluate the regex is chosen dynamically. --- src/JsonSchema/Constraints/ObjectConstraint.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index 67ae7d7d..84271fd6 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -42,15 +42,23 @@ function check($element, $definition = null, $path = null, $additionalProp = nul public function validatePatternProperties($element, $path, $patternProperties) { + $try = array('/','#','+','~','%'); $matches = array(); foreach ($patternProperties as $pregex => $schema) { + // Choose delimiter. Necessary for patterns like ^/ , otherwise you get error + foreach ($try as $delimiter) { + if (strpos($pregex, $delimiter) === false) { // safe to use + break; + } + } + // Validate the pattern before using it to test for matches - if (@preg_match('/'. $pregex . '/', '') === false) { + if (@preg_match($delimiter. $pregex . $delimiter, '') === false) { $this->addError($path, 'The pattern "' . $pregex . '" is invalid', 'pregex', array('pregex' => $pregex,)); continue; } foreach ($element as $i => $value) { - if (preg_match('/' . $pregex . '/', $i)) { + if (preg_match($delimiter . $pregex . $delimiter, $i)) { $matches[] = $i; $this->checkUndefined($value, $schema ? : new \stdClass(), $path, $i); } From 7099ef0f9366657f2a4e721b7acbb0cc9a9109b6 Mon Sep 17 00:00:00 2001 From: Paulo Ribeiro Date: Mon, 26 Oct 2015 14:10:12 -0200 Subject: [PATCH 2/3] tests added to bugfix: patternProperties raised errors when the pattern contained slash. --- .../Constraints/PatternPropertiesTest.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/JsonSchema/Tests/Constraints/PatternPropertiesTest.php b/tests/JsonSchema/Tests/Constraints/PatternPropertiesTest.php index ef053880..79cc7ffa 100644 --- a/tests/JsonSchema/Tests/Constraints/PatternPropertiesTest.php +++ b/tests/JsonSchema/Tests/Constraints/PatternPropertiesTest.php @@ -82,10 +82,29 @@ public function getValidTests() ), 'someotherobject' => array( 'foobar' => 1234, + ), + '/products' => array( + 'get' => array() + ), + '#products' => array( + 'get' => array() + ), + '+products' => array( + 'get' => array() + ), + '~products' => array( + 'get' => array() + ), + '*products' => array( + 'get' => array() + ), + '%products' => array( + 'get' => array() ) )), json_encode(array( 'type' => 'object', + 'additionalProperties' => false, 'patternProperties' => array( '^someobject$' => array( 'type' => 'object', @@ -100,6 +119,42 @@ public function getValidTests() 'foobar' => array('type' => 'number'), ), ), + '^/' => array( + 'type' => 'object', + 'properties' => array( + 'get' => array('type' => 'array') + ) + ), + '^#' => array( + 'type' => 'object', + 'properties' => array( + 'get' => array('type' => 'array') + ) + ), + '^\+' => array( + 'type' => 'object', + 'properties' => array( + 'get' => array('type' => 'array') + ) + ), + '^~' => array( + 'type' => 'object', + 'properties' => array( + 'get' => array('type' => 'array') + ) + ), + '^\*' => array( + 'type' => 'object', + 'properties' => array( + 'get' => array('type' => 'array') + ) + ), + '^%' => array( + 'type' => 'object', + 'properties' => array( + 'get' => array('type' => 'array') + ) + ) ) )) ), From 2ba8f952908a0ce10f118c53c2998e72a8cfcf45 Mon Sep 17 00:00:00 2001 From: Paulo Ribeiro Date: Wed, 2 Dec 2015 17:52:34 -0200 Subject: [PATCH 3/3] delimiter defaulted to a value before loop --- src/JsonSchema/Constraints/ObjectConstraint.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index 84271fd6..0e5cf1b9 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -45,6 +45,7 @@ public function validatePatternProperties($element, $path, $patternProperties) $try = array('/','#','+','~','%'); $matches = array(); foreach ($patternProperties as $pregex => $schema) { + $delimiter = '/'; // Choose delimiter. Necessary for patterns like ^/ , otherwise you get error foreach ($try as $delimiter) { if (strpos($pregex, $delimiter) === false) { // safe to use