diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index a0d1d8b8..ee55922f 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -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'; + } } diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index a55356d1..2620e7d6 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -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) diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index dd1c02b9..b4f85650 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -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)); } diff --git a/src/JsonSchema/Constraints/StringConstraint.php b/src/JsonSchema/Constraints/StringConstraint.php index b3bdfbf7..1ccb23b0 100644 --- a/src/JsonSchema/Constraints/StringConstraint.php +++ b/src/JsonSchema/Constraints/StringConstraint.php @@ -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, ));