Karate: 检查数组中的值是否为空或null

huangapple go评论57阅读模式
英文:

Karate: checking value in array whether any of the value is blank or null

问题

{
"result": [
{
"hostname": "CDGRPASEAAR001",
"critical": 0,
"minor": 1,
"syslog": 0
},
{
"hostname": "MUMBCHUDAAR001",
"critical": 0,
"minor": 0,
"syslog": 0
},
{
"hostname": "",
"critical": 0,
"minor": 1,
"syslog": 0
}
]
}

英文:

Here is the sample json

{
  "result": [
    {
      "hostname": "CDGRPASEAAR001",
      "critical": 0,
      "minor": 1,
      "syslog": 0
    },
    {
      "hostname": "MUMBCHUDAAR001",
      "critical": 0,
      "minor": 0,
      "syslog": 0
    },
    {
      "hostname": "",
      "critical": 0,
      "minor": 1,
      "syslog": 0
    },

I am trying below:

And match actualResponse == {result[2].hostname: '##notnull'}

But I am getting this validation message:

> reason: actual value has 4 more key(s) than expected

答案1

得分: 1

我认为这是你想要的:

* 匹配 response.result[2] 包含 { hostname: ''##notnull'' }

这也可以:

```plaintext
* 匹配每个 response.result 包含 { hostname: ''#string'' }

所以请查阅文档:https://github.com/karatelabs/karate#fuzzy-matching

如果你想要找到一些计数,那么 `match` 不是你想要的。你需要开始进行转换,比如 `filter()`。参考:https://stackoverflow.com/a/74459339/143475 | https://stackoverflow.com/a/75425063/143475

例如:

```plaintext
* 定义 names = response.result.filter(x => x.hostname !== '''').map(x => x.hostname)
* 匹配 names == ['CDGRPASEAAR001', 'MUMBCHUDAAR001']
英文:

I think this is what you were trying for:

* match response.result[2] contains { hostname: '##notnull' }

This would also work:

* match each response.result contains { hostname: '#string' }

So please go through the docs: https://github.com/karatelabs/karate#fuzzy-matching

If you are trying to find some count, then match is not what you want. You have to start doing transforms, like filter(). Refer: https://stackoverflow.com/a/74459339/143475 | https://stackoverflow.com/a/75425063/143475

For example:

* def names = response.result.filter(x => x.hostname !== '').map(x => x.hostname)
* match names == ['CDGRPASEAAR001', 'MUMBCHUDAAR001']

huangapple
  • 本文由 发表于 2023年4月13日 22:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定