diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index 67ae7d7d..0e5cf1b9 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -42,15 +42,24 @@ function check($element, $definition = null, $path = null, $additionalProp = nul 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 + 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); } 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') + ) + ) ) )) ),