2

I can have a response as:

 { id: '123', name: 'foo' }` 

if the user has not a dog

OR

{ id: '123', name: 'foo', dog: {id: '123', color: 'brown'} }`

if the user has a dog.

In my feature I have:

* def schema = { id: '#string', name: '#string', dog: {id: '#string', color: '#string'} }`
* match response == schema

The prb is that if I have a user without dog in response, I have this error:

path: $[0].dog, actual: null, expected: {id=#string, label=#string}, reason: actual value is null

How can I check the attribute 'dog' in my schema? Thanks

cygne
  • 527
  • 1
  • 10
  • 28

1 Answers1

1

It is not possible to do this in one step, also refer to the doc on schema validation:

* def dogSchema = { id: '#string', color: '#string' }
* def schema = { id: '#string', name: '#string', dog: '##object' }

* def response1 = { id: '123', name: 'foo' }
* match response1 == schema
* match response1 contains { dog: '##(dogSchema)' }

* def response2 = { id: '123', name: 'foo', dog: { id: '123', color: 'brown' } }
* match response2 == schema
* match response1 contains { dog: '##(dogSchema)' }

Edit: well this is embarassing, I realized this trick is not well documented:

* def dogSchema = { id: '#string', color: '#string' }
* def schema = { id: '#string', name: '#string', dog: "#('##(dogSchema)')" }

* def response1 = { id: '123', name: 'foo' }
* match response1 == schema

* def response2 = { id: '123', name: 'foo', dog: { id: '123', color: 'brown' } }
* match response2 == schema

Edit2: this will be improved in the next version: https://github.com/intuit/karate/issues/248

Also refer: https://stackoverflow.com/a/56987803/143475 and https://stackoverflow.com/a/70006027/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • thank you, it works. I've put the schema in a json file and put double ## here: `dog: "##('##(dogSchema)')"`. Two kind of responses can be validated. It's much easier than with Json Validation and much better. – cygne Nov 16 '17 at 21:16
  • @nirind this will be made easier in the next version: https://github.com/intuit/karate/issues/248 – Peter Thomas Nov 18 '17 at 06:34