# 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
- Expected Schema sub-tab under Validation tab of each test case
- 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 the 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 the Expected Schema sub-tab under the Response Validation tab as shown below:
Expected Schema for Add Contact API
Expected Schema for Update Contact API
Note: Both APIs consume the same JSON schema as the response structure of both APIs is the same.
Now if you execute these two test cases, then both will pass. Now if we have hundreds of such test cases, then defining the same schema in multiple test case(s) will make our Test Maintenance harder. To overcome this issue we may define the schema globally in the 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 to the global definitions defined in the 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 the 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 the version is swagger2.0
or if the version is not defined then
{
"$ref": "#/definitions/global_schema_name"
}
b. if the version is openapi3
then
{
"$ref": "#/components/schemas/global_schema_name"
}