JSON Schema Validation
JSON Schema Validation
● Defining the structure of JSON data
● JSON Schema is a vocabulary that allows you to
annotate and validate JSON documents
● Most basic schema is a blank JSON object {}
● { "type": "object" }
What is JSON?
What we can check in JSON Schema
1. We can check for the type of fields and fail the test if Type is
different
2. We can check for required fields
3. We can check the number of min propteroes should come in
response
4. We can verify with example data.
What we can check in JSON Schema
5. If we have additionalProperties : false, So Schema validation will fail if we have extra key added to response.
6. banUnknownProperties is will the test if you have unknown or extra field like additonalProperties
"7. You can use the Regex also in Properties like to verify field data type (e.g I am checking if property
proBinaryName has .exe or not)
{
""type"": ""object"",
""properties"": {
""progBinaryName"": {
""type"": ""string"",
""pattern"": ""^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$""
}
}
}"
How to do it in Postman
1. Copy the response to these sites
// Create the Schema using [Link]
// another tool - [Link]
// [Link]
2. create a variable like this
var schema = {); put into it
3. Use Tiny validate to verify with reponse data.
[Link]('JSON Schema', function() {
var jsonData = [Link]();
[Link]([Link](jsonData, schema)).[Link];
});
How to do it in Postman
In POSTMAN
[Link]
JSON Schema
● {}
● { "type": "string" }
● { "$schema": "[Link] }
● "minLength": 2,
● "maxLength": 3
● "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
● Properties
● Required
● items
JSON Schema
[Link]
[Link]
[Link]
[Link]
[Link]
How to do in REST ASSURED?
<dependency>
<groupId>[Link]-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.3.1</version>
</dependency>
Adding maven dependency of json-schema-validator
How to do in REST ASSURED?
<dependency>
<groupId>[Link]-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.3.1</version>
</dependency>
Adding maven dependency of json-schema-validator
import [Link];
[Link]("[Link]")
How to do in REST ASSURED?
[Link]
[Link]