首页 > 解决方案 > 检查数组是否仅包含一个对象,该对象包含提供的键子集

问题描述

给定以下输入:

* def list = [{a: 'aaa', b:'bbb'}, {a:'aa', c:'ccc'}]
* def anotherList = [{a: 'aaa', b:'bbb'}, {a:'aa', b:'', c:'ccc'}]
* def msg = {b: '#string'}

如何检查b密钥只存在一次?

使用:

* match list contains '#(^msg)'

b将验证在列表中至少出现一次。

如何检查是否恰好出现一次 key binlist或 in anotherList?所以这样的检查应该通过并且失败listanotherList

标签: karate

解决方案


match您可以使用 a和contains快捷方式进行过滤:

* def list = [{ a: 'aaa', b: 'bbb' }, { a: 'aa', c: 'ccc' }]
* def msg = { b: '#string' }
* def fun = function(x){ return karate.match(x, '#(^msg)').pass }
* def filt = karate.filter(list, fun)
* match filt == '#[1]'

推荐阅读