Skip to content
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

Streamline validation of patternProperties Regex #653

Merged
merged 3 commits into from
Mar 18, 2021
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
12 changes: 12 additions & 0 deletions src/JsonSchema/Constraints/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,16 @@ public static function arrayToObjectRecursive($array)

return (object) json_decode($json);
}

/**
* Transform a JSON pattern into a PCRE regex
*
* @param string $pattern
*
* @return string
*/
public static function jsonPatternToPhpRegex($pattern)
{
return '~' . str_replace('~', '\\~', $pattern) . '~u';
}
}
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected function validateDateTime($datetime, $format)

protected function validateRegex($regex)
{
return false !== @preg_match('#' . str_replace('#', '\\#', $regex) . '#u', '');
return false !== @preg_match(self::jsonPatternToPhpRegex($regex), '');
}

protected function validateColor($color)
Expand Down
13 changes: 3 additions & 10 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,17 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $prop

public function validatePatternProperties($element, JsonPointer $path = null, $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;
}
}
$fullRegex = self::jsonPatternToPhpRegex($pregex);

// Validate the pattern before using it to test for matches
if (@preg_match($delimiter . $pregex . $delimiter . 'u', '') === false) {
if (@preg_match($fullRegex, '') === false) {
$this->addError(ConstraintError::PREGEX_INVALID(), $path, array('pregex' => $pregex));
continue;
}
foreach ($element as $i => $value) {
if (preg_match($delimiter . $pregex . $delimiter . 'u', $i)) {
if (preg_match($fullRegex, $i)) {
$matches[] = $i;
$this->checkUndefined($value, $schema ?: new \stdClass(), $path, $i, in_array($i, $this->appliedDefaults));
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/StringConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
}

// Verify a regex pattern
if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#u', $element)) {
if (isset($schema->pattern) && !preg_match(self::jsonPatternToPhpRegex($schema->pattern), $element)) {
$this->addError(ConstraintError::PATTERN(), $path, array(
'pattern' => $schema->pattern,
));
Expand Down