I have imported a swagger schema with which I want to validate my requests. Everything works very well. However, I am missing detailed information when the validation fails with The response body didn’t match the specified schema, which is not very helpful.
My idea was to perform an AJV validation in the post-response to get detailed errors. Is there a way to access the imported schema in this context? Or do I only have the option to redefine the schema at this point?
const body = pm.response.json();
const collectionPageSchema = pm.getCurrentResponseSchemaOrSomething(); ????
or
const collectionPageSchema = pm.api.definition.paths[...] ???
pm.test("Is valid JSON:API", function () {
pm.expect(
ajv.validate(collectionPageSchema, body),
ajv.errorsText(ajv.errors)
).to.be.true;
});
The schema defined within the API builder is not programmatically available in Postman scripts. You’ll have to store that schema as a variable or in the package library and validate it using Ajv. Additionally, turning on request validation compares your response against the schema and gives you appropriate warnings on what may be wrong.
No, my hope was to get the schema programmatically from the already imported API builder an make a custom validation in a test-script. But it seems the solution is to repeat the schema in the test-script again.
This is no approach for me. Never repeat yourself in coding