regexkarateweb-api-testing

Karate API - Using regex to match numbers


I want to assert the returned values in my response using a regex statement.

["num_items": 1000, "num_items": 1111]

Currently I have "#regex \\d(1000|1111)".

Which throws the error "$[0].num_items | not a string (NUMBER:STRING)"

How can I change my regex statement to assert a number and not a string?


Solution

  • If you already have a number those are easier to validate:

    * match each response contains { num_items: '#? _ == 1000 || _ == 1001' }
    

    But you can always do a type-conversion first:

    * def nums = response.map(x => x.num_items + '')
    * match each nums == '#regex \\d(1000|1111)'
    

    Refer docs: https://github.com/karatelabs/karate#type-conversion