http responce 断言验证 JSONschema
JSONschema 一般在使用 比较复杂的数据交互时,使用. 如 游戏中, 小怪攻击力.等等. 与 xml, yaml ,csv 等并列为全平台通用数据格式; Python 中 ijson 可以加载大量 JSON 格式数据. json 为一般情况下的数据.
{
"code":0,
"message":"Success",
"results":[
{
"id":"44",
"bemm_change":"company_name31231",
"change_date":"2019-07-08",
"change_project":"经营范围",
"after_change":"31231",
"before_change":"31231",
"hash_id":"random...",
"create_time":"2021-11-05",
"update_time":"2021-11-05",
"record_time":"2019-07-08",
"is_delete":"0"
},
{
"id":"45",
"bemm_change":"company_name31231",
"change_date":"2019-07-08",
"change_project":"法定代表人",
"after_change":"31231",
"before_change":"31231",
"hash_id":"random...",
"create_time":"2021-11-05",
"update_time":"2021-11-05",
"record_time":"2019-07-08",
"is_delete":"0"
},
{
"id":"46",
"bemm_change":"company_name31231",
"change_date":"2019-07-08",
"change_project":"董事(理事)、经理、监事",
"after_change":"31231",
"before_change":"31231",
"hash_id":"random...",
"create_time":"2021-11-05",
"update_time":"2021-11-05",
"record_time":"2019-07-08",
"is_delete":"0"
}
]
}
验证语句
// method1 限制 ID 以及 ID 个数, 左右闭区间;
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"required": [ "results"],
"type": "object",
"properties": {
"results": {
"type": "array",
"minItems": 20, // results 提取 ID, 限制最小为 20
"maxItems": 30, // results 提取 ID, 限制最大为 30
"pattern":"/id/"
}
}
}
// method2 只验证 返回值message, 并且 包含字符串success即可(不区分大小写)
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"required": [ "message"],
"type": "object",
"properties": {
"msg": {
"type": "string",
"pattern":"/success/"
}
}
}
使用资料 JSONSchema keywords-for-arrays
基本概念: js和python类型参照表: