Schema Validation

Schema Validation is also a powerful feature of vREST. You may validate your API test case's responses against JSON schema. You may define this JSON schema in

  1. Expected Schema sub-tab under Validation tab of each test case
  2. And in Configuration >> Schemas section

Now we will see, how you may validate your test cases using JSON Schema definition.

1. Create test cases for your test application

  • Suppose we have two test cases for our example test application (Contacts Application).
  • One test case is validating the Add Contact API and another test case is validating the Update Contact API.
  • A sample screenshot is available below:

2. Validate those test cases via JSON Schema by defining the schema in Expected Schema Tab

  • Now you may validate these test cases by defining the expected JSON schema for the test cases responses.
  • You can define the JSON schema in Expected Schema sub-tab under Response Validation tab as shown below:

Expected Schema for Add Contact API

Expected Schema for Update Contact API

Note: Both APIs consumes the same JSON schema as the response structure of both APIs are same.

Now if you execute these two test cases, then both will pass. Now if we have hundreds of such test cases, then we will be defining the same schema in each test case. To overcome this issue we may define the schema globally in Project Configuration tab and re-use the same schema in our test cases.

3. Re-use the common schema by defining them in Configuration >> Schemas Section

In Configuration >> Schema Section, you may define all your schemas related to your APIs. Then you may use them in your test cases for response validation. In your test cases, you just need to refer the global definitions defined in Schemas section. This section is also useful when you import test cases via swagger definitions.

Let's define our Contact schema in Configuration >> Schemas Section as shown below:

Now, we may use this schema in our test cases by referencing it in Expected Schema tab of our test cases as shown below:

Updated Expected Schema for Add Contact API:

Updated Expected Schema for Update Contact API:

Note: If you are using Swagger 2 Schema or OpenAPI 3 Schema in vREST NG

1. You will need to add the version property in the schema object

a. For Swagger 2 schema definition:

{
  "version": "swagger2.0",
  "type": "object",
  ...
}

b. For OpenAPI 3 schema definition:

{
  "version": "openapi3",
  "type": "object",
  ...
}

2. To reference global schemas

a. if version is swagger2.0 then

{
  "$ref": "#/definitions/global_schema_name"
}

b. if version is openapi3 then

{
  "$ref": "#/components/schemas/global_schema_name"
}