Skip to content

Commit c7b3eb0

Browse files
authored
Fixed json matcher when matching strings including other patterns (#188)
1 parent 26f53fc commit c7b3eb0

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

src/Matcher/JsonMatcher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public function match($value, $pattern) : bool
3131
return false;
3232
}
3333

34-
$transformedPattern = Json::transformPattern($pattern);
34+
$transformedPattern = Json::isValid($pattern) ? $pattern : Json::transformPattern($pattern);
3535
$match = $this->matcher->match(\json_decode($value, true), \json_decode($transformedPattern, true));
36+
3637
if (!$match) {
3738
$this->error = $this->matcher->getError();
3839
return false;

src/Matcher/Pattern/Assert/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function isValidPattern($value) : bool
2828
return false;
2929
}
3030

31-
return self::isValid(self::transformPattern($value));
31+
return self::isValid($value) || self::isValid(self::transformPattern($value));
3232
}
3333

3434
public static function transformPattern(string $pattern) : string

tests/Matcher/JsonMatcherTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public static function positivePatterns()
151151
[\json_encode(['Norbert', 'Michał'])],
152152
[\json_encode(['Norbert', '@string@'])],
153153
[\json_encode('test')],
154+
[\json_encode(['foo' => '/foo/@uuid@/bar'])],
154155
];
155156
}
156157

tests/MatcherTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public function jsonDataProvider()
131131
"nextPage": "@string@"
132132
}',
133133
],
134+
'matches with txt matcher ' => [
135+
/** @lang JSON */
136+
'{"foo": "/foo/92ff9a6c-7fbd-47e5-b550-90b77e3284c7/bar"}',
137+
/** @lang JSON */
138+
'{"foo": "/foo/@uuid@/bar"}'
139+
],
134140
'matches json values with full text matcher' => [
135141
/** @lang JSON */
136142
'{

0 commit comments

Comments
 (0)