Skip to content

Commit 38ceb74

Browse files
author
Michael Chiocca
committed
Fix regex format validation and fix the regex format unit tests.
1 parent 62aef11 commit 38ceb74

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/JsonSchema/Constraints/Format.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public function check($element, $schema = null, $path = null, $i = null)
5656
break;
5757

5858
case 'regex':
59-
$this->validateRegex($path, $element, $schema->pattern);
59+
if (!$this->validateRegex($element)) {
60+
$this->addError($path, 'Invalid regex format ' . $element);
61+
}
6062
break;
6163

6264
case 'color':
@@ -124,13 +126,9 @@ protected function validateDateTime($datetime, $format)
124126
return $datetime === $dt->format($format);
125127
}
126128

127-
protected function validateRegex($path, $element, $regex)
129+
protected function validateRegex($regex)
128130
{
129-
if (false === @preg_match('/' . $regex . '/', '')) {
130-
$this->addError($path, 'Invalid regex format ' . $regex);
131-
} else if (!preg_match('/' . $regex . '/', $element)) {
132-
$this->addError($path, sprintf('%s does not match regex format %s', json_encode($element), $regex));
133-
}
131+
return false !== @preg_match('/' . $regex . '/', '');
134132
}
135133

136134
protected function validateColor($color)

tests/JsonSchema/Tests/Constraints/FormatTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ public function testRegex()
2727
$validator = new Format();
2828
$schema = new \stdClass;
2929
$schema->format = 'regex';
30-
$schema->pattern = '\d+';
3130

32-
$validator->check('10', $schema);
31+
$validator->check('\d+', $schema);
3332
$this->assertEmpty($validator->getErrors());
3433

35-
$validator->check('ten', $schema);
34+
$validator->check('^(abc]', $schema);
3635
$this->assertCount(1, $validator->getErrors());
3736
}
3837

0 commit comments

Comments
 (0)