2

I would like to test that the number of decimal places returned by a field in the response is equal to 5. Not sure if using regex is the best solution but this is what I have so far which does not seem to work:

pm.test("Check number of decimal place of my_variable field"), () => {
    _.each(jsonData, (results) => {
        if results.my_variable !== null){
            pm.expect(results.my_variable).to.match(/\d{1}.\d{5}/);
        }
    })
}

Below is a sample response. Note that the test passes even if it does not match the regex

 {
     "my_variable": "0.0198970000000000",

 }

Thanks!

kz500
  • 21
  • 2

1 Answers1

0

Two things concerning your regex:

You need to escape the . with a \, otherwise the . will be interpreted as "any single character".

\d{5} is matching for 5 digits, but doesn't care if there are more digits following, thus you need to add $ which checks if the end of line is reached, if nothing more is following the 5 digits.

This regex is working: \d{1}\.\d{5}$

Christian Baumann
  • 1,921
  • 3
  • 15
  • 26