Skip to content

Commit 7c78776

Browse files
committed
Merge pull request #194 from ribeiropaulor/justinrainbow/master
bugfix: patternProperties raised errors when the pattern has slashes
2 parents 77b7e38 + 2ba8f95 commit 7c78776

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,24 @@ function check($element, $definition = null, $path = null, $additionalProp = nul
4242

4343
public function validatePatternProperties($element, $path, $patternProperties)
4444
{
45+
$try = array('/','#','+','~','%');
4546
$matches = array();
4647
foreach ($patternProperties as $pregex => $schema) {
48+
$delimiter = '/';
49+
// Choose delimiter. Necessary for patterns like ^/ , otherwise you get error
50+
foreach ($try as $delimiter) {
51+
if (strpos($pregex, $delimiter) === false) { // safe to use
52+
break;
53+
}
54+
}
55+
4756
// Validate the pattern before using it to test for matches
48-
if (@preg_match('/'. $pregex . '/', '') === false) {
57+
if (@preg_match($delimiter. $pregex . $delimiter, '') === false) {
4958
$this->addError($path, 'The pattern "' . $pregex . '" is invalid', 'pregex', array('pregex' => $pregex,));
5059
continue;
5160
}
5261
foreach ($element as $i => $value) {
53-
if (preg_match('/' . $pregex . '/', $i)) {
62+
if (preg_match($delimiter . $pregex . $delimiter, $i)) {
5463
$matches[] = $i;
5564
$this->checkUndefined($value, $schema ? : new \stdClass(), $path, $i);
5665
}

tests/JsonSchema/Tests/Constraints/PatternPropertiesTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,29 @@ public function getValidTests()
8282
),
8383
'someotherobject' => array(
8484
'foobar' => 1234,
85+
),
86+
'/products' => array(
87+
'get' => array()
88+
),
89+
'#products' => array(
90+
'get' => array()
91+
),
92+
'+products' => array(
93+
'get' => array()
94+
),
95+
'~products' => array(
96+
'get' => array()
97+
),
98+
'*products' => array(
99+
'get' => array()
100+
),
101+
'%products' => array(
102+
'get' => array()
85103
)
86104
)),
87105
json_encode(array(
88106
'type' => 'object',
107+
'additionalProperties' => false,
89108
'patternProperties' => array(
90109
'^someobject$' => array(
91110
'type' => 'object',
@@ -100,6 +119,42 @@ public function getValidTests()
100119
'foobar' => array('type' => 'number'),
101120
),
102121
),
122+
'^/' => array(
123+
'type' => 'object',
124+
'properties' => array(
125+
'get' => array('type' => 'array')
126+
)
127+
),
128+
'^#' => array(
129+
'type' => 'object',
130+
'properties' => array(
131+
'get' => array('type' => 'array')
132+
)
133+
),
134+
'^\+' => array(
135+
'type' => 'object',
136+
'properties' => array(
137+
'get' => array('type' => 'array')
138+
)
139+
),
140+
'^~' => array(
141+
'type' => 'object',
142+
'properties' => array(
143+
'get' => array('type' => 'array')
144+
)
145+
),
146+
'^\*' => array(
147+
'type' => 'object',
148+
'properties' => array(
149+
'get' => array('type' => 'array')
150+
)
151+
),
152+
'^%' => array(
153+
'type' => 'object',
154+
'properties' => array(
155+
'get' => array('type' => 'array')
156+
)
157+
)
103158
)
104159
))
105160
),

0 commit comments

Comments
 (0)