邮递员:更具描述性的 tv4 验证错误消息 [英] Postman: More descriptive tv4 validation error message
问题描述
我正在使用 postman 来验证从 api 返回的 json 数据的架构.
I'm using postman to validate the schema of json data returned from an api.
我有一个通过基本 http 验证运行的测试,然后以:
I have a test that runs through basic http validation, then ends with:
if (tv4.error){
console.log("Validation failed: ", tv4.error);
}
我返回的错误很难理解.
The error I get back is difficult to fathom.
验证失败:12:22:41.316
对象:{}
消息:无效类型:数字(预期字符串)"
名称:验证错误"
类型:错误"
Validation failed: 12:22:41.316
Object:{}
message:"Invalid type: number (expected string)"
name:"ValidationError"
type:"Error"
但我需要知道验证失败的字段.我怎样才能得到这些信息?tv4 的 npm 页面 表明错误消息应该更具描述性.
But I need to know which field the validation failed on. How can I get this info? The npm page for tv4 suggests that the error message should be more descriptive.
推荐答案
根据tv4的文档,可以使用console.log(tv4.error.dataPath)
,我不知道为什么这个属性没有记录在控制台中.
According to the documentation of tv4, you can print the path of the error location using console.log(tv4.error.dataPath)
, I have no idea why this attribute is not logged in the console.
文档位于此处.文档中的相关部分是:
Documentation is here. The relevant section in the documentation is:
如果验证返回 false,那么验证失败的原因可以在 tv4.error 中找到.
If validation returns false, then an explanation of why validation failed can be found in tv4.error.
错误对象看起来像:
{
"code": 0,
"message": "Invalid type: string",
"dataPath": "/intKey",
"schemaPath": "/properties/intKey/type"
}
这篇关于邮递员:更具描述性的 tv4 验证错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!