我正在嘗試使用空手道來驗證這兩種狀態中的任何一種的 API 回應。場景 1(當它回傳一個包含費用密鑰的 contractData 物件時):
{
"customer": {
"financialData": {
"totalAmount": 55736.51,
"CreateDate": "2022-04-01",
"RequestedBy": "[email protected]"
},
"contractData": {
"Fee": 78.00
}
}
}
場景 2(當它回傳一個空的 contractData 物件時):
{
"customer": {
"financialData": {
"totalAmount": 55736.51,
"CreateDate": "2022-04-01",
"RequestedBy": "[email protected]"
},
"contractData": {}
}
}
如何撰寫模式驗證邏輯來??驗證這兩種狀態?我能做的最好的事情就是這樣寫:
* def schema = {"customer":{"financialData":{"totalAmount":"#number","CreateDate":"#?isValidDate(_)","RequestedBy":"#string"},"contractData":{"Fee": ##number}}}
* match response == schema
似乎它適用于上述兩種情況,但我不確定這是否是最好的方法。這種方法的問題是,如果我在“contractData”物件中有多個鍵:值對,并且我想確保當它不為空時所有這些鍵都存在于其中,我無法通過這種方法檢查它,因為對于每個人鍵:值對,這種方法假設它們可能存在或不存在,并且即使其中一些鍵存在,也會匹配模式。
uj5u.com熱心網友回復:
哇,我不得不承認我從來沒有遇到過這種情況,這說明了一些事情。我終于能夠想出一個可能的解決方案:
* def chunk = { foo: 'bar' }
* def valid = function(x){ return karate.match(x, {}).pass || karate.match(x, chunk).pass }
* def schema = { hey: '#? valid(_)' }
* def response1 = { hey: { foo: 'bar' } }
* def response2 = { hey: { } }
* match response1 == schema
* match response2 == schema
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/455077.html
上一篇:從嵌套的JSON中獲取資料
