ruchira more
ruchira more

Reputation: 79

How to validate response structure in a template using regular expression in Karate API testing

I am trying to validate response structure and response value using a json template. I am able to validate a key that might have different predefined values using regular expression.Yet the DateTime i am unable to validate by providing regular expression in the template.

For example:

API response to be validated:

t1 = {
  "key1": "ABC",
  "DateTime" : "2018-01-30T14:00:00+00:00"
}

Template for validation:

t2 = {
  "key1": "#regex(ABC|PQR|XYZ|AAA)$",,
  "DateTime" : "#regex \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2]\d|3[0-1])T(?:[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d[+-]([01]\d|2[0-4])(:?[0-5]\d)?"
}

* match t1 == t2

Please let me know what i am doing wrong and how can i achieve the datetime validation through regex template matching.

Thanks

Upvotes: 1

Views: 741

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

You have to escape the \ character. Read the docs: https://github.com/intuit/karate#ignore-or-validate

* def foo = '2018-01-30T14:00:00+00:00'
* match foo == '#regex \\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2]\\\d|3[0-1])T(?:[0-1]\\\d|2[0-3]):[0-5]\\\d:[0-5]\\\d[+-]([01]\\\d|2[0-4])(:?[0-5]\\\d)?'

Upvotes: 1

Related Questions