# API Schema Validation

### Overview

API schema validation in scriptless automation enables automated testing of API responses against predefined schema templates. It ensures that both the structure and types of values in the response body match expected patterns, helping to maintain robust API behavior.

#### API Request

<figure><img src="/files/kbUxh4u0IzQRUSDQNx1p" alt=""><figcaption></figcaption></figure>

#### **Test Template Example**

Here’s an example of an API test template:

```csv
DEPENDANT_TEST_CASE,NONE
END_POINT,https://api-generator.retool.com/vEQvcT/data
METHOD,POST
PARAMS:KEY,NONE
PARAMS:VALUE,NONE
AUTH:KEY,NONE
AUTH:VALUE,NONE
HEADERS:KEY,Content-Type
HEADERS:VALUE,application/json
BODY:KEY,JsonRepository,name
BODY:VALUE,test,Custom:CustomClass:body
RESPONSE:CODE,201
RESPONSE:SCHEMA,putSchema.json
RESPONSE:JSON_PATH,'Column 1',id
RESPONSE:EXPECTED_VALUE,ApiGlobalVariables:name,NONE
RESPONSE:STORE_VALUE,Name,ID
```

This test template outlines the steps for performing a POST API call, setting headers, body, and verifying the response against a schema file named `putSchema.json`.

### **Schema Validation**

The schema validation works by comparing the actual API response with a predefined schema file located in the automation's test data directory. The schema provides the expected structure and data types for dynamic validation.

**Schema Example (putSchema.json)**

```
{
  "Column 1": "ApiGlobalVariables:name",
  "id": "@Integer"
}
```

* `Column 1`: Maps to a dynamic variable, i.e., `ApiGlobalVariables:name`.
* `id`: Expected to be an integer, indicated by the placeholder `@Integer`.

The test will validate the structure and data types against the API response. If the response body matches the expected schema, the test passes.

### **Validation Flow**

1. **Compare JSON Paths:**\
   The automation tool will take each key from the expected schema and compare it with the actual response from the API. It validates both the structure and the value types of the fields to ensure consistency between the schema and the response.
2. **Dynamic Value Validation:**\
   If the schema contains placeholders such as `@integer` or `@string`, the automation validates the data types of the response fields instead of exact values. For example, if the schema specifies `"id": "@Integer"`, the test will check that the value for `id` in the response is an integer.
3. **Direct Value Validation:**\
   If the schema contains a static value (e.g., `"status": "active"`), the automation will directly validate the actual value from the response against the expected value in the schema. This ensures that predefined static values are correct in the API response.
4. **Global Variable Validation:**\
   If the schema contains dynamic values using `ApiGlobalVariables:<Key>`, the automation will fetch the value from the global variables stored in the automation framework. The value associated with the specified `<Key>` will then be validated against the corresponding value in the API response. For example:
   * Schema: `"Column 1": "ApiGlobalVariables:name"`
   * The automation retrieves the value of `name` from the global variables and validates it against the `Column 1` field in the response.
5. **Report Validation:**\
   The results of schema validation, whether they pass or fail, are logged and added to the test report. If a schema mismatch is detected (such as a data type error or incorrect value), the report will include detailed information on the differences, helping to identify the source of the error. This information ensures that discrepancies are easily traceable for debugging and fixing.

<figure><img src="/files/KjrBK3i9rO4XMRfjEfwX" alt=""><figcaption></figcaption></figure>

### **Handling Missing Schema**

If no schema is provided for validation, the automation will log a message to indicate that schema validation was skipped:

```
Schema validation is not mentioned in the template
```

### **Supported Data Types**

The following dynamic data types are supported for schema validation:

* `INTEGER`
* `STRING`
* `FLOAT`
* `BOOLEAN`
* `DATE`
* `ARRAY`
* `JSON_OBJECT`
* `UUID`

These types can be used in schema files to indicate the expected format of the API response fields.

### **Best Practices**

* **Dynamic Variables:** Use dynamic variables like `ApiGlobalVariables` to manage state across test cases and ensure flexible validation.
* **Data Type Placeholders:** Ensure you replace static values with dynamic placeholders (`@integer`, `@string`, etc.) when validating non-static data in API responses.
* **Error Logging:** Always ensure that errors during schema validation are captured in the test report for easier debugging.

### **Conclusion**

This schema validation approach streamlines API testing by allowing dynamic type validation and comparison against predefined structures. By incorporating flexible schema rules, you can maintain robust test automation for APIs with varying data responses.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.precisiontestautomation.in/scriptlessautomation/automation-platforms/scriptlessapi-automation/api-schema-validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
