空手道比赛表情

时间:2020-06-10 12:14:47

标签: karate

我正在使用架构验证来验证响应,值返回一个数字或“ NA”,下面是响应和架构验证。

Response:
{
    "ID": "ES123D74590Z",
    "companyName": "ABC Corp",
    "hourMs": 67890000000,
    "date": "2020-06-09T00:00:00.000Z",
    "scores": {
        "AllScore": 61,
        "MaxScore": 59,
        "ScoreA": 75,
        "ScoreB": "NA",
        "ScoreC": 49,
        "ScoreD": "NA"
    },
    "movement": {},
    "amt": {}
}
Schema Assertion:
{
    "ID": '#string',
    "companyName": '#string',
    "hourMs": '#number',
    "date": '#regex[^\d\d\d\d-([0-9]{2})-([0-9]{2})(\T)([0-9]{3}):([0-9]{3}):([0-9]{3})\.[0-9]{3}\Z)$]',
    "scores": {
        "AllScore": '##number? _ >= 0 && _ <=100 || _ == "NA"',
        "MaxScore": '##number? _ >= 0 && _ <=100 || _ == "NA"',
        "ScoreA": '##number? _ >= 0 && _ <=100 || _ == "NA"',
        "ScoreB": '##number? _ >= 0 && _ <=100 || _ == "NA"',
        "ScoreC": '##number? _ >= 0 && _ <=100 || _ == "NA"',
        "ScoreD": '##number? _ >= 0 && _ <=100 || _ == "NA"'
    },
    "movement": {},
    "amt": {}
}

收到错误消息:

com.intuit.karate.exception.KarateException: score.feature:19 - path: $.scores.ScoreB, actual: 'NA', expected: '##number? _ >= 0 && _ <=100 || _ == "NA"', reason: not a number

如何纠正匹配表达式?

1 个答案:

答案 0 :(得分:1)

您在这里。尝试在别人的帮助下查看您的代码。并仔细阅读文档。下次,这样简化您的问题。

* def response =
"""
{
    "AllScore": 61,
    "MaxScore": 59,
    "ScoreA": 75,
    "ScoreB": "NA",
    "ScoreC": 49,
    "ScoreD": "NA"
}
"""
* def isNum = function(x){ if (x === 'NA') return true; return typeof x === 'number' }
* def schema =
"""
{
    "AllScore": '#? isNum(_)',
    "MaxScore": '#? isNum(_)',
    "ScoreA": '#? isNum(_)',
    "ScoreB": '#? isNum(_)',
    "ScoreC": '#? isNum(_)',
    "ScoreD": '#? isNum(_)'
}
"""
* match response == schema

我还建议您查看此日期验证示例以获取更多想法:https://stackoverflow.com/a/55938480/143475