如何使用空手道框架检查 json 模式中存在的属性值

How to check values of an attribute if it is present in json schema using karate framework

我可以这样回复:

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

如果用户没有狗

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

如果用户有狗。

在我的功能中我有:

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

prb 是如果我有一个没有狗的用户响应,我有这个错误:

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

如何检查架构中的属性 'dog'? 谢谢

一步完成是不可能的,另请参考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)' }

编辑:嗯,这很尴尬,我意识到这个技巧没有很好的记录:

* 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:下个版本会改进:https://github.com/intuit/karate/issues/248