I've did a lot of tests but I coulnd't find the way to solve it. Simplifying, I have this Postman test script to validate that the JSON response es according with the JSON schema defined to this API:
const stockQuotesSchema = JSON.parse(pm.environment.get("schema"));
pm.test("Stock quote returns 200 OK", function () {
pm.response.to.have.status(200);
})
pm.test("Stock quote is JSON", function () {
pm.response.to.be.json;
})
pm.test("Stock quote response matches schema", function () {
const validationResult = tv4.validateResult(pm.response.json(), stockQuotesSchema);
pm.expect(validationResult.valid).to.be.true;
})
This is the API schema defined (simplified):
{
"codexxx": "UNAUTHENTICATED",
"messagexxx": "token expired"
}
This is the response I get after run the request:
{
"code": "UNAUTHENTICATED",
"message": "token expired"
}
As the "code" field and the "message" field doesn't exist in the schema I hope to get a FAIL, but I always retrieve a True.
This is the Postman result image
I need to validate every response with a long JSON schema (I mean bigger schema than the example above). Any idea? Thanks.
You can enforce the proposed (and then removed)
banUnknownPropertiesmode through additional arguments tovalidateResultas described here. For example:which outputs:
The third argument to
validateResultspecifies whether to do recursive checking. It's not immediately clear, but I believe that the default is false (as provided in the example above).