karate - 检查数组是否仅包含一个对象,该对象包含提供的键子集
问题描述
给定以下输入:
* 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 b
inlist
或 in anotherList
?所以这样的检查应该通过并且失败。list
anotherList
解决方案
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]'
推荐阅读
- python - 在 Python 中生成填充有唯一整数值的列表
- dart - Flutter - 如何访问父事件或动作的子数据
- javascript - 使用过滤器从数组中删除悬停的项目
- android - 我想在我的 activity.java 文件中使用 ToolBar,并且此工具栏必须位于仅包含 ListView 的 layout.xml 文件中。我该怎么做?
- orm - 如何使用 XML 或 YAML 而非注解映射来定义 Doctrine mappedSuperclass
- django-rest-framework - 在 DRF 中动态设置 renderer_class
- spring-boot - .transform / .compose 使用 Spring Security 重复执行 Mono
- mysql - 在 MySQL 数据库模式中仅显示已填充表的列表
- python - 交易API数据采集
- google-data-studio - getData 请求中缺少 dateRange