JsonSchema 启蒙

jsonSchema 的应用场景有很多,毕竟现在各个接口传输数据基本都是json,比如你做测试想对部分json字段进行校验或者统计你该如何写?解析json获取字段然后if else?
不是说不可以但是也太low了,完全可以用jsonSchema来解决这个问题。有人就会说了,可是我不会啊,不会写jsonSchema,也不知道怎么用。 没关系问题不大,几分钟就能搞定的问题
首先让我们取一个json 样例数据

{
    "task_id": "f0f951a8-9883-11e9-a4ac-f0000aff48a4",
    "success": 1000,
    "result": {
        "code": 1000,
        "bad_cnt": 0,
        "doubtList": [{
            "phone": "13366179991",
            "name": "贷款",
            "key": ""
        }],
        "small_rate": 0.0,
        "invalid_cnt": 11,
        "phone_small": {
            "18611516150": false,
            "13240350826": true
        }
    }
}

然后打开网址 https://app.quicktype.io/#l=schema  输入json生成schema
如果你仔细观察一下的话你会发现,结构其实和你的json差不多是对应的,但是还不能直接用,下面我们只需要对照这个schema做一点修改就可以投入使用了

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Welcome",
    "definitions": {
        "Welcome": {    # 名字随便起
            "type": "object",
            "additionalProperties": false,  # 这里要改成true
            "properties": {
                "task_id": {                #task_id 字段的相关验证
                    "type": "string",       #数据类型  字符串
                    "format": "uuid"        #数据格式uuid
                },
                "success": {
                    "type": "integer"       #数据类型 int
                },
                "result": {
                    "$ref": "#/definitions/Result"  # result 字段的相关验证对应下面的Result
                }
            },
            "required": [  #表示需要校验的字段,比如我们只需要校验 'success' 可以把别的删除
                "result",   
                "success",
                "task_id"
            ],
            "title": "Welcome"
        },
        "Result": {       # 这是是result字段的校验信息
            "type": "object", 
            "additionalProperties": false,  # 改成true
            "properties": {
                "code": {
                    "type": "integer"
                },
                "bad_cnt": {
                    "type": "integer"
                },
                "doubtList": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/DoubtList"
                    }
                },
                "small_rate": {
                    "type": "integer"
                },
                "invalid_cnt": {
                    "type": "integer"
                },
                "phone_small": {
                    "type": "object",
                    "additionalProperties": {
                        "type": "boolean"
                    }
                }
            },
            "required": [
                "bad_cnt",
                "code",
                "doubtList",
                "invalid_cnt",
                "phone_small",
                "small_rate"
            ],
            "title": "Result"
        },
        "DoubtList": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "phone": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "key": {
                    "type": "string"
                }
            },
            "required": [
                "key",
                "name",
                "phone"
            ],
            "title": "DoubtList"
        }
    }
}

好,现在开始正式干活

a = {
    "task_id": "f0f951a8-9883-11e9-a4ac-f0000aff48a4",
    "success": 1000,    #比如我们现在要检验这个字段必须是1000,具体字段条件咨询百度,
    "result": {
        "code": 1000,
        "bad_cnt": 0,
        "doubtList": [{
            "phone": "13366179991",
            "name": "贷款",
            "key": ""
        }],
        "small_rate": 0.0,
        "invalid_cnt": 11,
        "phone_small": {
            "18611516150": false,
            "13240350826": true
        }
    }
}
#那我们修改schema
s = {
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$ref": "#/definitions/Welcome",
  "definitions": {
    "Welcome": {
      "type": "object",
      "additionalProperties": True,
      "properties": {
        "success": {
            "type": "integer",
            "enum":[1000]  #限定枚举值
        }
      },
      "required": [
        "success"
      ],
      "title": "Welcome"
    }
  }
}
from jsonschema import validate
try:
    validate(s,a)
    print('ok')
except:
    print('error')

学会了没有?没学会不要紧我们再来一次

a = {
    "task_id": "f0f951a8-9883-11e9-a4ac-f0000aff48a4",
    "success": 1000,   
    "result": {
        "code": 1000,
        "bad_cnt": 0,
        "doubtList": [{
            "phone": "13366179991",
            "name": "贷款",
            "key": ""
        }],
        "small_rate": 0.0,  # 我们要验证这个字段是float 最小值是0
        "invalid_cnt": 11,
        "phone_small": {
            "18611516150": false, #顺便验证这个字段是boolean,而且只能是True
            "13240350826": true
        }
    }
}
那么schema应该这么改:
{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Welcome",
    "definitions": {
        "Welcome": {
            "type": "object",
            "additionalProperties": True,
            "properties": {
                "result": {
                    "$ref": "#/definitions/Result"
                }
            },
            "required": [
                "result"
            ],
            "title": "Welcome"
        },
        "Result": {
            "type": "object",
            "additionalProperties": True,
            "properties": {
                "small_rate": {
                    "type": "number",
                    "minimum": 0,
                    "exclusiveMinimum": True
                },
                "phone_small": {
                    "type": "object",
                    "additionalProperties": {
                        "type": "boolean",
                        "enum":[True]
                    }
                }
            },
            "required": [
                "phone_small",
                "small_rate"
            ],
            "title": "Result"
        }
    }
}

OK轻松加愉快,照葫芦画瓢就行了。什么?你还想知道别的字段限制条件?有肯定是有的,但是我不会说,因为说了你也记不住,具体用的时候自行百度。

posted @ 2019-07-24 17:35  君子不徒语  阅读(353)  评论(0编辑  收藏  举报