Schema validation of an API request can be used to validate if the API returns the expected response. It is being used widely in consumer contract testing.
Let’s modify a bit the example of the request from our previous post: http://trikalabs.com/postman-mock-server/.
- Change the example response to the following array:
1234567891011121314151617[{"registrationNumber" : "ABCD11","make": "BMW","model": "z1"},{"registrationNumber" : "ABCD12","make": "BMW","model": "z1"},{"registrationNumber" : "ABCD13","make": "BMW","model": "z1"}] - Now let’e select the test tab:
- And write our schema:
1234567891011121314151617const jsonSchema = {"type": "array","items": {"type": "object","Properties": {"registrationNumber": {"type": "string"},"make": {"type": "string"},"model": {"type": "string"}}}}; - And write our validation test
123pm.test("Schema Validation", () => {pm.response.to.have.jsonSchema(jsonSchema);}); - And let’s run it:
Test is passing!