A popular format for RESTful API schema formats is JSON Schema. It's a well designed spec that is extensible, easy to read and write, and supports canonical and inline referencing/dereferencing. We’ve recently shipped an update that gives Runscope Radar assertion support for JSON Schema validation by way of the Chai assertion library.
So, what exactly does that mean? It means that you’re able to use Runscope API tests to validate your API responses against a defined JSON Schema. A few examples of validation include:
insuring method responses are structured properly
checking for existence of required keys
verifying that values are of a certain type (e.g. integer, string, etc.) and within constraints (min/max)
Runscope API tests currently supports validation against the v4 draft spec of JSON Schema. Lets run through an example.
Step 1: Define the Schema
The method we’re testing pulls down the roster of players on a hockey team. The top level object is an array named roster. Each array item of roster contains an object named player, which has three required properties: player_number, guid and name. So, in this first step, we provide our Runscope test with the JSON schema.
We’ll store the schema as an environment variable via the Initial Script (a script that executes before the request steps begin). At line 1, we’ll store the JSON Schema into a local variable named rosterResponse. In order to make this globally accessible, we’ll store an object using variables.set on line 25. (link to source)
Note: This sample schema uses canonical references (see reference to player object). They're quite handy, especially when you're working in a larger schema with repeated use of an object.
Step 2: Add Request Step and Script Assertion
When we run the test, the results show our schema validation test as passing.
To try this out on your own account, or to read more about JSON Schema and the Chai JSON-Schema module, see the links below:
If you need assistance setting up validation against your JSON Schema or general help with Runscope contact our support team at any time.