json schema主要用来验证json的格式,在测试过程中的返回格式验证上面,涉及比较多,下面就来做一个总结性的说明和介绍。
1. json的结构类型
如下图,json有以下几种数据结构,而json shema的验证也是基于这几种结构来进行验证:
- object:
{ "key1": "value1", "key2": "value2" }
array:
[ "first", "second", "third" ]
number:
42 3.1415926
string:
"This is a string"
boolean:
true false
null:
null
2. 示例介绍
1)schema校验格式
{ "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "birthday": { "type": "string", "format": "date" }, "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" }, "country": { "type" : "string" } } } } }
2)返回错误的校验
{ "name": "George Washington", "birthday": "February 22, 1732", "address": "Mount Vernon, Virginia, United States" }
3)返回正确的校验
{ "first_name": "George", "last_name": "Washington", "birthday": "22-02-1732", "address": { "street_address": "3200 Mount Vernon Memorial Highway", "city": "Mount Vernon", "state": "Virginia", "country": "United States" } }
4)详解
如示例所示,schema类型定义了一个完整的json校验格式,以及其中细分的属性校验。 例如,定义first_name、last_name字段为string类型,而错误的示例,则没有相应的字段 定义birthday为date类型,如正确的示例,而错误的示例虽然字段名一致,但类型校验未通过。
通过上面的说明,可以明确json schema是对json数据的一种校验和约束,可以很方便的判断某个json是否符合自定义的格式要求。
3. 高级应用
通过上面的例子可以知道,json schema用起来比较方便,可以深入到每个字段层级进行定义,但是假如遇到庞大数量的json,如果要挨个校验,未免会有些过于繁琐,于是可以想到相对应的,有没有可替代的方式,答案当然是有的:
1)重用
定义如下schema的校验:
{ "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] }
使用上述schema的引用:
{ "$ref": "definitions.json#/address" }
重用上述schema:
{ "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] } }, "type": "object", "properties": { "billing_address": { "$ref": "#/definitions/address" }, "shipping_address": { "$ref": "#/definitions/address" } } }
以下结果便可以通过校验:
{ "shipping_address": { "street_address": "1600 Pennsylvania Avenue NW", "city": "Washington", "state": "DC" }, "billing_address": { "street_address": "1st Street SE", "city": "Washington", "state": "DC" } }
2)递归重用
schema示例:
{ "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "person": { "type": "object", "properties": { "name": { "type": "string" }, "children": { "type": "array", "items": { "$ref": "#/definitions/person" }, "default": [] } } } }, "type": "object", "properties": { "person": { "$ref": "#/definitions/person" } } }
对应的校验示例:
{ "person": { "name": "Elizabeth", "children": [ { "name": "Charles", "children": [ { "name": "William", "children": [ { "name": "George" }, { "name": "Charlotte" } ] }, { "name": "Harry" } ] } ] } }
3)组合模式
- anyof(符合任一条件)
{ "anyOf": [ { "type": "string", "maxLength": 5 }, { "type": "number", "minimum": 0 } ] }
校验结果:
正确的校验: "hello" 3 错误校验: "ehqijosjqiehcduw" -1
allof(符合所有条件)
{ "allOf": [ { "type": "string" }, { "maxLength": 5 } ] }
校验结果:
正确结果: "hello" 错误结果: 123
oneof(其中之一符合,不包含全部,仅选其一)
{ "oneOf": [ { "type": "number", "multipleOf": 5 }, { "type": "number", "multipleOf": 3 } ] }
校验结果:
正确结果: 9 10 错误结果: 2 15
not(不符合)
{ "not": { "type": "string" } }
校验结果:
正确结果: 9 错误结果: "hello"
4)混合使用
{ "definitions": { "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] } }, "allOf": [ { "$ref": "#/definitions/address" }, { "properties": { "type": { "enum": [ "residential", "business" ] } } } ] }
正确校验:
{ "street_address": "1600 Pennsylvania Avenue NW", "city": "Washington", "state": "DC", "type": "business" }
以上便是json shema的用法总结,欢迎关注交流~