JSON Scheme Introduction
In response to a request for data or JSON-formatted data, there is often a part of the dynamic and the value of field data and different scenarios. Then we can use JSON Scheme Validator (JSON structure authentication) to verify the structure of JSON, nesting parameters and type of each parameter, and the necessary fields.
Such as: GET http://httpbin.org/get?a=a
The response data:
{ "args": { "a": "a" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Cache-Control": "no-cache", "Host": "httpbin.org", "Postman-Token": "08abebe1-eaa4-46a2-9b3a-0f2a5580c44f", "User-Agent": "PostmanRuntime/7.15.0" }, "origin": "164.52.33.194, 164.52.33.194", "url": "https://httpbin.org/get?a=a" }
we can verify the structure:
- Is a whole (type of) object object contains attributes args, headers, origin, url, essential fields (fields must be present) if all
- args type of object, attribute comprising a, a type of string
- headers of type object, comprising property Accept, Accept-Encoding, Cache-Control, Host, Postman-Token, User-Agent, which are of type string
- origin of type string
- url of type string
Into JSON Schema syntax is as follows:
{
"type": "object",
"properties": {
"args": {"type": "object","properties": {"a": {"type": "string"}} },
"hearders": {
"type": "object",
"properties": {
"Accept": {"type": "string"},
"Cache-Control": {"type": "string"},
"Host": {"type": "string"},
"Postman-Token": {"type": "string"},
"User-Agent": {"type": "string"},
},
},
"origin": {"type": "string"},
"url": {"type": "string"},
},
"required": ["args", "headers", "origin", "url"]
}
object type validation general format:
{
"type": "object",
"properties": {...}
"required": [....]
}
Where type is specified as the type of object, write each sub-attributes properties, required elements must fill to appear, required for specified elements may not be present, but it appears it must be of the type defined
array type of validation for the general format:
{
"type": "array",
"items": {...}
"required": [....]
}
Which type of type array, write each child under the items, required elements must fill to appear.
type of authentication string format:
{"type": "string"}
integer format type of authentication:
{"type": "integer"}
JSON Scheme also supports grammar references, and many other assignments, reference may be made: http://json-schema.org/
Postman tv4 use
tv4 i.e. Tiny Validator for JSON data Abbreviation, JSON micro structure verifier.
Postman in use is also very simple, first write JSON Schema template structure Tests The response script, then tv3.validate (jsonData, schema) can be verified, as shown below:
Tests code is as follows:
var schema = {
"type": "object",
"properties": {
"args": {"type": "object", "properties": {"a": {"type": "string"}}},
"hearders": {
"type": "object",
"properties": {
"Accept": {"type": "string"},
"Cache-Control": {"type": "string"},
"Host": {"type": "string"},
"Postman-Token": {"type": "string"},
"User-Agent": {"type": "string"},
},
},
"origin": {"type": "string"},
"url": {"type": "string"},
},
"required": ["args", "headers", "origin", "url"]
}
pm.test('Schema is valid', function() {
var jsonData = pm.response.json();
pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
Run can be seen, the assertion by: