Skip to content

Commit 24aa1a9

Browse files
jbaron-gingcobighappyface
authored andcommitted
[BUGFIX] Add unicode support in patternProperties and in format constraints (#338)
1 parent a942b98 commit 24aa1a9

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function validateDateTime($datetime, $format)
146146

147147
protected function validateRegex($regex)
148148
{
149-
return false !== @preg_match('/' . $regex . '/', '');
149+
return false !== @preg_match('/' . $regex . '/u', '');
150150
}
151151

152152
protected function validateColor($color)

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p
5656
}
5757

5858
// Validate the pattern before using it to test for matches
59-
if (@preg_match($delimiter. $pregex . $delimiter, '') === false) {
59+
if (@preg_match($delimiter. $pregex . $delimiter . 'u', '') === false) {
6060
$this->addError($path, 'The pattern "' . $pregex . '" is invalid', 'pregex', array('pregex' => $pregex,));
6161
continue;
6262
}
6363
foreach ($element as $i => $value) {
64-
if (preg_match($delimiter . $pregex . $delimiter, $i)) {
64+
if (preg_match($delimiter . $pregex . $delimiter . 'u', $i)) {
6565
$matches[] = $i;
6666
$this->checkUndefined($value, $schema ? : new \stdClass(), $path, $i);
6767
}

tests/Constraints/FormatTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ public function testRegex()
3333
$schema = new \stdClass;
3434
$schema->format = 'regex';
3535

36+
$validator->reset();
3637
$validator->check('\d+', $schema);
3738
$this->assertEmpty($validator->getErrors());
3839

40+
$validator->reset();
3941
$validator->check('^(abc]', $schema);
4042
$this->assertCount(1, $validator->getErrors());
43+
44+
$validator->reset();
45+
$validator->check('^猡猡獛$', $schema);
46+
$this->assertEmpty($validator->getErrors());
4147
}
4248

4349
/**

tests/Constraints/PatternPropertiesTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public function getInvalidTests()
5252
"additionalProperties" => false
5353
))
5454
),
55+
// Does not match pattern with unicode
56+
array(
57+
json_encode(array(
58+
'猡猡獛' => false,
59+
)),
60+
json_encode(array(
61+
'type' => 'object',
62+
'patternProperties' => array(
63+
'^[\\x{0080}-\\x{006FFF}]+$' => array(
64+
'type' => array('boolean')
65+
)
66+
),
67+
"additionalProperties" => false
68+
))
69+
),
5570
// An invalid regular expression pattern
5671
array(
5772
json_encode(array(
@@ -177,6 +192,21 @@ public function getValidTests()
177192
"additionalProperties" => false
178193
))
179194
),
195+
// Does match pattern with unicode
196+
array(
197+
json_encode(array(
198+
'ðæſ' => 'unicode',
199+
)),
200+
json_encode(array(
201+
'type' => 'object',
202+
'patternProperties' => array(
203+
'^[\\x{0080}-\\x{10FFFF}]+$' => array(
204+
'type' => array('string')
205+
)
206+
),
207+
"additionalProperties" => false
208+
))
209+
),
180210
);
181211
}
182212
}

0 commit comments

Comments
 (0)