Skip to content

[BUGFIX] Add unicode support in patternProperties and in format constraints #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected function validateDateTime($datetime, $format)

protected function validateRegex($regex)
{
return false !== @preg_match('/' . $regex . '/', '');
return false !== @preg_match('/' . $regex . '/u', '');
}

protected function validateColor($color)
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p
}

// Validate the pattern before using it to test for matches
if (@preg_match($delimiter. $pregex . $delimiter, '') === false) {
if (@preg_match($delimiter. $pregex . $delimiter . 'u', '') === false) {
$this->addError($path, 'The pattern "' . $pregex . '" is invalid', 'pregex', array('pregex' => $pregex,));
continue;
}
foreach ($element as $i => $value) {
if (preg_match($delimiter . $pregex . $delimiter, $i)) {
if (preg_match($delimiter . $pregex . $delimiter . 'u', $i)) {
$matches[] = $i;
$this->checkUndefined($value, $schema ? : new \stdClass(), $path, $i);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Constraints/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ public function testRegex()
$schema = new \stdClass;
$schema->format = 'regex';

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

$validator->reset();
$validator->check('^(abc]', $schema);
$this->assertCount(1, $validator->getErrors());

$validator->reset();
$validator->check('^猡猡獛$', $schema);
$this->assertEmpty($validator->getErrors());
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Constraints/PatternPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public function getInvalidTests()
"additionalProperties" => false
))
),
// Does not match pattern with unicode
array(
json_encode(array(
'猡猡獛' => false,
)),
json_encode(array(
'type' => 'object',
'patternProperties' => array(
'^[\\x{0080}-\\x{006FFF}]+$' => array(
'type' => array('boolean')
)
),
"additionalProperties" => false
))
),
// An invalid regular expression pattern
array(
json_encode(array(
Expand Down Expand Up @@ -177,6 +192,21 @@ public function getValidTests()
"additionalProperties" => false
))
),
// Does match pattern with unicode
array(
json_encode(array(
'ðæſ' => 'unicode',
)),
json_encode(array(
'type' => 'object',
'patternProperties' => array(
'^[\\x{0080}-\\x{10FFFF}]+$' => array(
'type' => array('string')
)
),
"additionalProperties" => false
))
),
);
}
}
Expand Down