Pebermynte Lars
Pebermynte Lars

Reputation: 487

Response object templates in Karate

Is there a way to create a template structure of a response object, that ignores content of fields? I'm in this instance interested in verifying that all relevant field names are present, regardless of related data. A dataless version of Templates that can be used in Scenario Outline would be great.

For the curious soul, I'm verifying API docs.

Example response to verify structure of:

"Clinics": [
{
  "ClinicId": 1212,
  "MondayOpen": null,
  "MondayClose": null,
  "TuesdayOpen": null,
  "TuesdayClose": null,
  "WednesdayOpen": null,
  "WednesdayClose": null,
  "ThursdayOpen": null,
  "ThursdayClose": null,
  "FridayOpen": null,
  "FridayClose": null,
  "SaturdayOpen": null,
  "SaturdayClose": null,
  "SundayOpen": null,
  "SundayClose": null,
}]

I'm not interested in the value of ClinicId or the other fields. I'd like to know if the field ClinicId, MondayOpen, and so on is present.

Upvotes: 1

Views: 973

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

I'm not sure I understand but let me try. If you have a JSON as follows:

* def json = { foo: 'bar', baz: 'ban' }

You can use the #present marker as follows:

* match json == { foo: '#present', baz: '#present' }

Now, it sounds like you want to dynamically determine the keys you want:

* def keys = ['foo', 'baz']

You can easily programmatically create the needed template:

* def template = {}
* eval karate.forEach(keys, function(k){ template[k] = '#present' })
* match json == template

Hope that helps ! Note that karate.forEach() is new in 0.8.0 - and you can use 0.8.0.RC9 in the mean-time.

If you refer these examples, you may get more ideas: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/search/search-complex.feature#L27

Upvotes: 1

Related Questions