Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman: More descriptive tv4 validation error message

I'm using postman to validate the schema of json data returned from an api.

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.

Validation failed: 12:22:41.316
Object:{}
message:"Invalid type: number (expected string)"
name:"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.

like image 871
powlo Avatar asked Jul 21 '17 11:07

powlo


People also ask

What is validation error in Postman?

Postman indicates validation errors on your API schema as you edit it in the Definition tab on the API version page. Errors can include missing required fields, malformed field names, incorrect data types, incorrect nesting, or other API schema validation issues.

What is JSON validation error?

This means that you have an error with the JSON data structure that you included in the body of your request and that it doesn't follow the format in which we expected to receive it. In order to know exactly what is wrong with your JSON data, you will need to validate it using a JSON schema.

What is API validation error?

Validation errors typically occur when a request is malformed -- usually because a field has not been given the correct value type, or the JSON is misformatted.


1 Answers

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:

If validation returns false, then an explanation of why validation failed can be found in tv4.error.

The error object will look something like:

{
    "code": 0,
    "message": "Invalid type: string",
    "dataPath": "/intKey",
    "schemaPath": "/properties/intKey/type"
}
like image 56
code4j Avatar answered Nov 08 '22 23:11

code4j