使用 tv4 的模式响应始终有效
Postman always valid as true a response with the schema using tv4
我做了很多测试,但我找不到解决问题的方法。简化一下,我有这个 Postman 测试脚本来验证 JSON 响应是否符合为此 API:
定义的 JSON 模式
const stockQuotesSchema = JSON.parse(pm.environment.get("schema"));
pm.test("Stock quote returns 200 OK", function () {
pm.response.to.have.status(200);
})
pm.test("Stock quote is JSON", function () {
pm.response.to.be.json;
})
pm.test("Stock quote response matches schema", function () {
const validationResult = tv4.validateResult(pm.response.json(), stockQuotesSchema);
pm.expect(validationResult.valid).to.be.true;
})
这是API定义的模式(简化):
{
"codexxx": "UNAUTHENTICATED",
"messagexxx": "token expired"
}
这是我在 运行 请求后得到的响应:
{
"code": "UNAUTHENTICATED",
"message": "token expired"
}
由于架构中不存在 "code" 字段和 "message" 字段,我希望得到一个 FAIL,但我总是检索到一个 True。
This is the Postman result image
我需要使用长 JSON 模式验证每个响应(我的意思是比上面的示例更大的模式)。任何的想法?谢谢
您可以强制执行建议(然后删除)banUnknownProperties
mode through additional arguments to validateResult
as described here。例如:
const tv4 = require('tv4');
const schema = {
properties: {
codexxx: {
type: 'string'
},
messagexxx: {
type: 'string'
}
}
};
const invalidResponse = {
code: 'UNAUTHENTICATED',
message: 'token expired'
};
const validResponse = {
codexxx: 'UNAUTHENTICATED',
messagexxx: 'token expired'
};
const invalidRelaxedResult = tv4.validateResult(invalidResponse, schema);
const validRelaxedResult = tv4.validateResult(validResponse, schema);
const invalidStrictResult = tv4.validateResult(invalidResponse, schema, false, true);
const validStrictResult = tv4.validateResult(validResponse, schema, false, true);
console.log('Relaxed (invalid):', invalidRelaxedResult.valid);
console.log('Relaxed (valid):', validRelaxedResult.valid);
console.log('Strict (invalid):', invalidStrictResult.valid,
invalidStrictResult.error.message, invalidStrictResult.error.dataPath);
console.log('Strict (valid):', validStrictResult.valid);
输出:
Relaxed (invalid): true
Relaxed (valid): true
Strict (invalid): false Unknown property (not in schema) /code
Strict (valid): true
validateResult
的第三个参数指定是否进行递归检查。目前还不清楚,但我相信默认值是 false(如上例所示)。
我做了很多测试,但我找不到解决问题的方法。简化一下,我有这个 Postman 测试脚本来验证 JSON 响应是否符合为此 API:
定义的 JSON 模式const stockQuotesSchema = JSON.parse(pm.environment.get("schema"));
pm.test("Stock quote returns 200 OK", function () {
pm.response.to.have.status(200);
})
pm.test("Stock quote is JSON", function () {
pm.response.to.be.json;
})
pm.test("Stock quote response matches schema", function () {
const validationResult = tv4.validateResult(pm.response.json(), stockQuotesSchema);
pm.expect(validationResult.valid).to.be.true;
})
这是API定义的模式(简化):
{
"codexxx": "UNAUTHENTICATED",
"messagexxx": "token expired"
}
这是我在 运行 请求后得到的响应:
{
"code": "UNAUTHENTICATED",
"message": "token expired"
}
由于架构中不存在 "code" 字段和 "message" 字段,我希望得到一个 FAIL,但我总是检索到一个 True。
This is the Postman result image
我需要使用长 JSON 模式验证每个响应(我的意思是比上面的示例更大的模式)。任何的想法?谢谢
您可以强制执行建议(然后删除)banUnknownProperties
mode through additional arguments to validateResult
as described here。例如:
const tv4 = require('tv4');
const schema = {
properties: {
codexxx: {
type: 'string'
},
messagexxx: {
type: 'string'
}
}
};
const invalidResponse = {
code: 'UNAUTHENTICATED',
message: 'token expired'
};
const validResponse = {
codexxx: 'UNAUTHENTICATED',
messagexxx: 'token expired'
};
const invalidRelaxedResult = tv4.validateResult(invalidResponse, schema);
const validRelaxedResult = tv4.validateResult(validResponse, schema);
const invalidStrictResult = tv4.validateResult(invalidResponse, schema, false, true);
const validStrictResult = tv4.validateResult(validResponse, schema, false, true);
console.log('Relaxed (invalid):', invalidRelaxedResult.valid);
console.log('Relaxed (valid):', validRelaxedResult.valid);
console.log('Strict (invalid):', invalidStrictResult.valid,
invalidStrictResult.error.message, invalidStrictResult.error.dataPath);
console.log('Strict (valid):', validStrictResult.valid);
输出:
Relaxed (invalid): true
Relaxed (valid): true
Strict (invalid): false Unknown property (not in schema) /code
Strict (valid): true
validateResult
的第三个参数指定是否进行递归检查。目前还不清楚,但我相信默认值是 false(如上例所示)。