简体   繁体   English

邮递员:更具描述性的tv4验证错误消息

[英]Postman: More descriptive tv4 validation error message

I'm using postman to validate the schema of json data returned from an api. 我正在使用邮递员来验证从api返回的json数据的模式。

I have a test that runs through basic http validation, then ends with: 我有一个测试,通过基本的http验证,然后结束:

if (tv4.error){
    console.log("Validation failed: ", tv4.error);
}

The error I get back is difficult to fathom. 我得到的错误很难理解。

Validation failed: 12:22:41.316 验证失败:12:22:41.316
Object:{} 宾语:{}
message:"Invalid type: number (expected string)" 消息:“无效的类型:数字(预期的字符串)”
name:"ValidationError" 名称: “ValidationError”
type:"Error" 类型:“错误”

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. tv4npm页面表明错误消息应该更具描述性。

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. 根据tv4的文档,你可以使用console.log(tv4.error.dataPath)打印错误位置的路径,我不知道为什么这个属性没有记录在控制台中。

Documentation is here . 文档在这里 The relevant section in the documentation is: 文档中的相关部分是:

If validation returns false, then an explanation of why validation failed can be found in tv4.error. 如果验证返回false,则可以在tv4.error中找到验证失败原因的解释。

The error object will look something like: 错误对象将类似于:

 { "code": 0, "message": "Invalid type: string", "dataPath": "/intKey", "schemaPath": "/properties/intKey/type" } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM