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.
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.
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.
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.
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" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With