8

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.

powlo
  • 2,538
  • 3
  • 28
  • 38

1 Answers1

6

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"
}
Community
  • 1
  • 1
code4j
  • 4,208
  • 5
  • 34
  • 51
  • 2
    The console did not log any dataPath by just logging the tv4 result: `var res = tv4.validateResult(data, schema); console.log("res",res);`. I had to do add .error.dataPath to actually see the content `console.log("path",res.error.dataPath);` – John Apr 16 '19 at 12:18