RegEx is matched even though it shouldn't be
cyuanli opened this issue · 3 comments
Hi everyone,
I'm rather new to Pact and still learning about its inner workings, so I'm not sure if I'm reporting this in the right repository. The issue I get is the following:
MISSION_e74c7e876e9a3f29f25068929085
is matched with ^[0-9a-zA-Z-]+
even though I forgot to specify the underscore in the string. Removing the capital letters (^[0-9a-z]+
) fails the test, as expected:
Failure/Error: expect(actual_contents).to match_term expected_contents, diff_options, example
Actual: {"missionId":{"id":"MISSION_e74c7e876e9a3f29f25068929085"}}
Diff
--------------------------------------
Key: - is expected
+ is actual
Matching keys and values are not shown
{
"missionId": {
- "id": /^[0-9a-z]+/
+ "id": "MISSION_e74c7e876e9a3f29f25068929085"
}
}
But for ^[0-9a-zA-Z-]+
it passes.
The error from Pact is correct, your regex, doesn't care about the underscore, its gets a match for MISSION
You can test it on https://regex101.com/
This seems like unintuitive behavior, passing the test if only a substring is matched. If I wanted to check a string for only containing alphanumeric characters, it would pass as long as the first character is a letter or digit? Is there a way of changing it such that the whole string needs to be matched?
Edit: figured it out, using $
works
You need to update your regex to not be so liberal, you can use the link provided to test it out in a playground 👍🏾