Photo of first indoor ice hockey game. (source: Wikipedia)
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 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 currently supports validation against the v4 draft spec of JSON Schema. Lets run through an example.
Step 1: Set the Schema Definition
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 global 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
Our Radar test has one GET request that returns the team roster. We’ll write our assertion in JavaScript that will validate the response body against the JSON Schema we defined in the previous step.
He Shoots.. He Scores!
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.
Update Addendum: Improvements to Scripted Assertions
As a side note, scripted assertions have also been improved! Fully evaluated details of each scripted assertion are written out on the test results, Also, all scripted assertions are executed, even if one of them fails. Previous to this update, the moment one assertion failed, the rest of the script would be skipped.
For a list of all updates to Runscope, visit our Change Log.