文章目录
-
- 1 介绍
- 2 [语法说明](https://json-schema.org/understanding-json-schema/index.html)
-
- 2.1 [$schema字段](https://json-schema.org/understanding-json-schema/reference/schema.html)
- 2.2 [通用关键字段](https://json-schema.org/understanding-json-schema/reference/generic.html)
- 2.2.1 注释字段
- 2.2.2 验证关键字
- 2.3 [特定类型关键字:type](https://json-schema.org/understanding-json-schema/reference/type.html)
- 2.4 [字符串 string](https://json-schema.org/understanding-json-schema/reference/string.html)
- 2.5 [数值类型](https://json-schema.org/understanding-json-schema/reference/numeric.html#multiples)
- 2.6 [对象object](https://json-schema.org/understanding-json-schema/reference/object.html)
- 2.7 [数组 array](https://json-schema.org/understanding-json-schema/reference/array.html)
- 2.8 [布尔值 boolean](https://json-schema.org/understanding-json-schema/reference/boolean.html)
- 2.9 [空值 null](https://json-schema.org/understanding-json-schema/reference/null.html)
- 2.10 [组合模式](https://json-schema.org/understanding-json-schema/reference/combining.html)
- 2.11 [逻辑结构](https://json-schema.org/understanding-json-schema/reference/conditionals.html)
- 2.12 [构建复杂的模式](https://json-schema.org/understanding-json-schema/structuring.html)
- 3 实战演练:生成json schema
- 4 集成至jmeter
1 介绍
Json Schema定义了一套词汇和规则,这套词汇和规则用来定义Json元数据,且元数据也是通过Json数据形式表达的。Json元数据定义了Json数据需要满足的规范,规范包括成员、结构、类型、约束等。
1.1 使用Json Schema做接口测试的步骤
- 将响应json转换成json schema格式,(在线转换工具:jsonschema.net)
- 优化修改生成的json schema,使其更符合该接口的各种场景;
- 校验修改后的json schema是否符合规范(在线校验工具:jsonschemalint.com/#!/version/draft-07/markup/json)。
- 进行断言操作。
1.2 优点
- 一个个参数的去验证,测试用例会非常多,代码也会很冗长。如果我们使用 json schema去验证的话,就会大大减少用例和代码数量。
- json schema描述json的数据格式,是一种元数据,它非常简单易读。只要返回的json符合json schema的要求,就可以通过测试。
- 多语言支持。
2 语法说明
2.1 $schema字段
- 字段说明:
字段 | 说明 | 备注 |
---|---|---|
$schema | 声明了针对该架构编写的JSON Schema标准版本。 | 如"$schema": “http://json-schema.org/draft-07/schema”, http://json-schema.org/draft/2019-09/schema# |
截止到目前,Json Schema一共有8个版本(版本迭代记录),最新的是Draft 8(2019-09)。
不同版本之间语法并不完全兼容,所以最佳实践是在写json schema的时候使用$schema关键字标记当前使用的是哪个规范。
2.2 通用关键字段
2.2.1 注释字段
- 字段说明:
字段 | 说明 | 备注 |
---|---|---|
$id | 架构关键字,定义模式的URI,并解析模式中其他URI引用的基URI。 | 如"$id": “http://example.com/example.json” |
title | 文档标题,关键字是描述性的,用来对文档作补充说明 | |
description | 文档描述,关键字是描述性的,用来对文档作补充说明 | |
default | 描述字段 | |
example | 描述字段,用于展示转换前的json | in draft 6 |
$comment | 描述字段,用于 schema 开发人员对文档进行注释,不需要展示给最终用户看。 | in draft 7 |
注:注释字段都是非必须字段。
- 举例:
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"description": "The root schema comprises the entire JSON document.",
"default": {
}
}
2.2.2 验证关键字
- 字段说明:
字段 | 说明 | 备注 |
---|---|---|
const | 验证值必须等于该常量,此关键字的值可以是任何类型,包括null。 | 如:“const”: 200,“const”: “success” 。注意,该关键字强类型语言不支持 |
enum | 验证枚举值,即值只能是enum数组中的某一项 | 如:“enum”: [200,404] |
- 举例1:const
{
"properties": {
"country": {
"const": "success"
}
}
}
{
"country": "success" } # pass
{
"country": "fail" } # fail
- 举例2:enum
{
"enum": ["red", "amber", "green", null, 42]
}
"red" # pass
null # pass
42 # pass
0 # fail
2.3 特定类型关键字:type
- 字段说明:
类型 | 说明 | 备注 |
---|---|---|
string | 字符串 | |
integer | 整型 | |
number | 数字 | |
object | 对象 | properties 关键字是必需的 |
boolean | 布尔值 | items 关键字是必需的 |
array | 列 | |
null | 空 | |
any | 任意 |
- 举例:
{
"type": "number" }
42 # pass
42.0 # pass
"42" # fail
2.4 字符串 string
- 字段说明:
字段 | 说明 | 备注 |
---|---|---|
maxLength | 字符串最大长度 | x <= maxLength |
minLength | 字符串最小长度 | x >= minLength |
pattern | 正则匹配字符串 | 如{“type”: “string”, “pattern”: “^(([0-9]{3}))?[0-9]{3}-[0-9]{4}$”} |
format | 字符串格式化引擎 | 这些是提案草稿内置的一些格式化校验方式见(#### 附1) |
contentMediaType | 指定MIME类型的字符串的内容 | 如字符串包含一个HTML文档:{“type”:“string”,“contentMediaType”:“text/html”} |
contentEncoding | 指定指定的编码用于存储内容 | 如字符串包含使用Base64编码的PNG图像:{“type”:“string”,“contentEncoding”:“base64”,“contentMediaType”:“image/png”} |
- 举例1:Length
{
"type": "string",
"minLength": 2,
"maxLength": 3
}
"A" # fail
"AB" # pass
"ABC" # pass
"ABCD" # fail
- 举例2:正则表达式
{
"type": "string",
"pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
}
"555-1212" # pass
"(888)555-1212" # pass
"(888)555-1212 ext. 532" # fail
"(800)FLOWERS" # fail
- 举例3:contentMediaType
{
"type": "string",
"contentMediaType": "text/html"
}
"<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head></html>" # pass
- 举例4:contentEncoding
{
"type": "string",
"contentEncoding": "base64",
"contentMediaType": "image/png"
}
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAAA..." # pass
附1:format内置有:
日期和时间
- “date-time”:例如 2018-11-13T20:20:39+00:00。
- “time”:例如,20:20:39+00:00
- “date”:例如,2018-11-13。
电子邮件地址
- “email”:互联网电子邮件地址,请参阅RFC 5322,第3.4.1节。
- “idn-email”:Internet电子邮件地址的国际化形式,请参阅 RFC 6531。
Hostnames
- “hostname”:互联网主机名,请参阅RFC 1034第3.1节。
- “idn-hostname”:国际化的Internet主机名,请参阅 RFC5890第2.3.2.3节。
IP地址 - “ipv4”:IPv4地址,根据RFC 2673第3.2节中定义的点分四进制ABNF语法。
- “ipv6”:IPv6地址,如RFC 2373第2.2节中所定义。
资源标识符
- “uri”:根据RFC3986的通用资源标识符(URI) 。
- “uri-reference”:草案6中的新增内容 URI参考(URI或相对参考),根据RFC3986第4.1节。
- “iri”:根据RFC3987,“ uri”的国际化等效项。
- “iri-reference”:根据RFC3987,“ uri-reference”的国际化等效项
如果架构中的值具有相对于特定源路径(例如,来自网页的链接)的能力,则通常更好的做法是使用 “uri-reference”(或"iri-reference")而不是"uri"(或 “iri”)。"uri"仅在路径必须为绝对路径时才应使用。
URI模板
- “uri-template”:根据RFC6570的 URI模板(任何级别) 。如果您尚不知道URI模板是什么,则可能不需要此值。
JSON指针
- “json-pointer”:在构造复杂的架构时,会更多地讨论在JSON架构中使用JSON指针。请注意,仅当整个字符串仅包含JSON指针内容时,才应使用此属性 /foo/bar。JSON指针URI片段,例如#/foo/bar/应使用 “uri-reference”。
- “relative-json-pointer”:相对JSON指针。
正则表达式
- “regex”:草案7中的新增内容一个正则表达式。
2.5 数值类型
- 数值类型包含integer(整形)和number(任何数字类型,整数或浮点数)。
- 字段说明:
字段 | 说明 | 备注 |
---|---|---|
multipleOf | 倍数 | |
minimum | 最小值 | x >= minimum |
maximum | 最大值 | x ≤ maximum |
exclusiveMinimum | 不包含最小值, 设置为true | false可以包含minimum的情况 |
exclusiveMaximum | 不包含最大值,设置为true | false可以包含maximum的情况 |
- 举例1:integer
{
"type": "integer"
}
42 # pass
-1 # pass
3.1415926 # fail
"42" # fail
- 举例2:number:
{
"type": "number",
"minimum": 0,
"exclusiveMaximum": 100
}
-1 # fail 少于minimum
0 # pass
10 # pass
99 # pass
100 # fail exclusiveMaximum 是排他性的,因此100无效
101 # fail 大于maximum
- 举例3:multipleOf
{
"type" : "number",
"multipleOf" : 10
}
0 # pass
10 # pass
20 # pass
23 # fail 不是10的倍数
- 举例4:精确小数点后几位
{
"type": "number",
"multipleOf": 1.0
}
42 # pass
42.0 # pass
3.14156926 # fail
2.6 对象object
- object(对象)是JSON中的映射类型。他们将“键”映射到“值”。在JSON中,“键”必须始终为字符串。这些对中的每对通常都称为“属性”。
- 字段说明:
字段 | 说明 | 备注 |
---|---|---|
properties | json串出现的属性(字段) | 每个属性又可以按照嵌套的方式使用 |
required | 存放必要属性列表。json串必须出现required要求的属性 | 通常用于判断响应中的必要字段 |
additionalProperties | true:json串允许properties之外的属性出现;false:json串不可以出现properties之外的属性; | 默认情况,允许出现其他属性 |
propertyNames.pattern | 可以根据模式验证属性的名称的合法性 | |
minProperties | 最少属性数量 | x >= minProperties |
maxProperties | 最大属性数量 | X <= maxProperties |
dependencies | 定义Key属性依赖的Value属性 | 如{“dependencies”: {“a”: [“b”]}}表示a属性必须依赖于b属性存在,才可以存在, |
patternProperties | 所有属性名称可以定义为一个正则 |
- 举例1:additionalProperties限制出现属性
如,json串为:
{
"status": 200,
"msg": "success"
}
转换成json schema:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"status",
"msg"
],
"additionalProperties": true,
"properties": {
"status": {
"type": "integer"
},
"msg": {
"type": "string"
}
}
}
-
当设置additionalProperties = true时,原json的status同级再增加一个属性,也能被校验通过:
-
但当设置为false时,就会校验失败,报错"should NOT have additional properties":
-
举2:additionalProperties限制属性类型
可以允许其他属性,但前提是每个属性都是一个字符串:
{
"type": "object",
"properties": {
"number": {
"type": "number" },
"street_name": {
"type": "string" },
"street_type": {
"type": "string",
"enum": ["Street", "Avenue", "Boulevard"]
}
},
"additionalProperties": {
"type": "string" }
}
{
"number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue" } # pass
{
"number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue", "direction": "NW" } # pass 附加属性的值是一个字符串
{
"number": 1600, "street_name": "Pennsylvania", "street_type": "Avenue", "office_number": 201 } # fail 附加属性的值不是字符串
- 举例3:propertyNames.pattern关键字说明:
如:
{
"type": "object",
"propertyNames": {
"pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
}
}
{
"_a_proper_token_001": "value"} # pass