I’m probably being dense here, but please bear with me.
My question:
I have created an API, by importing the following Swagger/OpenAPI 2.0 file:
https://raw.githubusercontent.com/tmforum-apis/TMF641_ServiceOrder/master/TMF641-ServiceOrdering-v4.1.0.swagger.json
It imported just fine: I allowed it to import as both collection + API.
However, when I create a client request in the Collection, the request is not validated against the Swagger schema (e.g. I can add nonsense top-level fields, or wrong data types for fields, and they are happily sent out).
Equally, if I create a Mock endpoint, the received request is not validated either. It just tells me it can’t find any matching mock. Of course, I can create matching mocks for specific valid requests, but I would like requests which are not valid according to the schema to be rejected.
Am I missing something obvious here? It seems to be a simple enough requirement, that if I’ve loaded an OpenAPI schema, that Postman can tell me whether the request I’ve prepared in the client collection is valid or not.
I’ve already tried:
I did find a blog post about validating responses, using the “Tests” feature. It suggested I added the following under the API “Tests” tab:
pm.test("Validate schema", () => {
pm.response.to.have.jsonSchema(schema);
});
But as far as I can see from the manual, tests are only run after the response has been received.
So instead, I tried creating a pre-request script:
pm.request.to.have.jsonSchema(schema);
This tells me that ‘schema’ is not defined. So I added above this:
var schema={
"swagger": "2.0",
"info": {
"title": "API ServiceOrdering",
...snip entire large swagger doc...
};
This runs, but the (invalid) request is still happily sent out. I changed it to:
console.log("XXX:" + pm.request.to.have.jsonSchema(schema));
and I get XXX:[object Object]
in the console.
If I change it to
console.log("XXX:" + pm.request.to.have.jsonSchema(schema).keys().join());
then I get “AssertionError: keys required”. As this point, I suspect I don’t know enough about the syntax of this “fluent” testing API.
It’s also quite likely that I’m misunderstanding the difference between JSON schema and Swagger/OpenAPI.
If this is a FAQ, or in the documentation please do point me at it - but I’ve done quite a lot of searching already and couldn’t find anything. There’s plenty of documentation about how to validate the Swagger API document itself, as opposed to validating requests and responses against the Swagger API doc. But I don’t need to validate the Swagger API doc: this is already provided to me in its final state and I won’t be changing it.
Many thanks for taking the time to read this!