空手道比赛表情
Karate match expression
我正在使用模式验证来验证响应,值 returns 一个数字或 '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
如何更正匹配表达式?
给你。尝试在某人的帮助下检查您的代码。并仔细阅读文档。下次,像这样简化你的问题。
* 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
此外,我建议您查看此日期验证示例以获得更多想法:
我正在使用模式验证来验证响应,值 returns 一个数字或 '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
如何更正匹配表达式?
给你。尝试在某人的帮助下检查您的代码。并仔细阅读文档。下次,像这样简化你的问题。
* 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
此外,我建议您查看此日期验证示例以获得更多想法: