我正在使用邮递员来验证从api返回的json数据的架构。
我有一个通过基本的HTTP验证运行的测试,然后以:
if (tv4.error){
console.log("Validation failed: ", tv4.error);
}
Run Code Online (Sandbox Code Playgroud)
我回来的错误很难理解。
验证失败:12:22:41.316
对象:{}
消息:“无效的类型:数字(预期的字符串)”
名称:“ ValidationError”
类型:“ Error”
有没有办法为 json 模式 (tv4) 设置自定义消息,以便在字段失败时使用oneOf?
我看到大约一年前在这里和这里出现了一个针对自定义消息的问题,但是有没有办法让这个问题适用于这样的事情?
{
"id": "code",
"description": "Schema for request.body - pin for logging into the bank",
"oneOf": [
{
"type": "string",
"pattern": "^.*\\S.*$"
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"pattern": "^(encrypted|not_encrypted)$"
},
"value": {
"type": "string",
"pattern": "^.*\\S.*$"
}
}
}
],
"messages": {
"oneOf": "Code does not match schema"
}
}
Run Code Online (Sandbox Code Playgroud)
与仅仅看到不同Data does not match any schemas from \"oneOf\",你可以看到Code does not match …
这就是我正在尝试的方法,但是即使结果不好也总是可以通过测试。
pm.test("Schema is valid", function () {
var data = pm.response.json();
var schema = {
...
my schema
...
};
tv4.validate(data, schema);
});
Run Code Online (Sandbox Code Playgroud) 我有一个示例回复:
{
"tags": [
{
"id": 1,
"name": "[String]",
"user_id": 1,
"created_at": "2016-12-20T15:50:37.000Z",
"updated_at": "2016-12-20T15:50:37.000Z",
"deleted_at": null
}
]
}
Run Code Online (Sandbox Code Playgroud)
我已经为响应写了一个测试:
var schema = {
"type": "object",
"properties": {
"tags": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"user_id": { "type": "number" },
"created_at": { "type": "string" },
"updated_at": { "type": "string" },
"deleted_at": { "type": ["string", "null"] }
}
}
}
};
var data = JSON.parse(responseBody);
tests["Valid schema"] = tv4.validate(data, schema);
Run Code Online (Sandbox Code Playgroud)
此测试返回[FAIL].测试中有什么错误? …