json schema如何约束为小数_如何使用jsonschema进行接口响应断言
一,JSONSchema的概念
JSONSchema是一種用來(lái)描述JSON數(shù)據(jù)的一種JSON數(shù)據(jù)結(jié)構(gòu)。JSON Schema也有版本,目前的版本有 Draft 7,Draft 6,Draft 4 和Draft 3。使用之前我們先進(jìn)行cmd安裝:pip install jsonschema
二,JSONSchema解析
2.1,type節(jié)點(diǎn)
string
number(int/float)
object(dict)
array(list)
boolean
null
2.2,properties
json以key--value的形式存在,實(shí)際就是描述存在的key值。
"properties":?{"name":?{"type":?"string"}2.3,propertyNames
實(shí)際是用來(lái)描述properties中key的信息,可用正則匹配進(jìn)行限制描述。
"properties":?{"name_123":?{"type":?"string","propertyNames":"^[A-Za-z_][A-Za-z0-9_]*$"}以上采用了key的名稱進(jìn)行限制,必須以字母開頭,且以"_"鏈接字母或字符的形式,否則將不符合規(guī)范。
2.4,required
required是用來(lái)描述json中必須存在哪些key。
data= { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "number"},????"gender":?{"type":?"string"}?? },????"required":?["name"]???#?要求檢測(cè)d的json數(shù)據(jù)必須包含name這個(gè)key??}2.5,enum
enum枚舉值限制,意思是json數(shù)據(jù)某個(gè)key的value值,只能是以下這幾個(gè)枚舉里的值。
"name":?{"type":?"string","enum":["張三","李四","王五"]}三,schema 實(shí)例
schema = { # 該關(guān)鍵字用于指定JSON Schema版本: draft-07????"$schema":?"http://json-schema.org/draft-07/schema#",????#?描述對(duì)應(yīng)的JSON元素????"description":?"這是一個(gè)比較齊全的schema校驗(yàn)",????#?該關(guān)鍵字用于限定待校驗(yàn)JSON元素所屬的數(shù)據(jù)類型????"type":?"object", "properties": { "name_id": { "description": "這是第一個(gè)一級(jí)key", "type": "integer", # 類型為數(shù)字????????????"minimum":?1???#?限制最小值為1 }, "name": {????????????"description":?"第二個(gè)一級(jí)key",????????????"type":?"string",????#?類型為字符串 "minLength": 3, # 最小長(zhǎng)度為3????????????"maxLength":?10????#?最大長(zhǎng)度為10 }, "class_name": {????????????"description":?"一級(jí)key用來(lái)描述班級(jí)名稱", "type": "string",????????????"minLength":?4,????????????"maxLength":?10 }, "skill": { "anyOf": [ # 滿足其中一個(gè)類型 就行????????????????{"type":?"string",?"minLength":?3,?"maxLength":?20},????????????????{"type":?"number",?"minimum":?5} ] },????????"money":?{ "type": "int",????????????#?能被1000整除????????????"multipleOf":?1000,????????????"minimum":?1000,????????????"maximum":?100000,????????????#?"exclusiveMinimum":?999,最小不包含999????????????#?"exclusiveMaximum":?99999,最大不包含99999 }, "tags": { "type": "array", "items": [ { "type": "string", "minLength": 2, "maxLength": 8 }, { "type": "number", "minimum": 1.0 } ], # 待校驗(yàn)JSON數(shù)組第一個(gè)元素是string類型,且可接受的最短長(zhǎng)度為5個(gè)字符,第二個(gè)元素是number類型,且可接受的最小值為10 # 剩余的其他元素是string類型,且可接受的最短長(zhǎng)度為2。 "additonalItems": { "type": "string", "miniLength": 2 }, # 至少一個(gè) "miniItems": 1, # 最多5個(gè) "maxItems": 5, # 值為true時(shí),所有元素都具有唯一性時(shí),才能通過(guò)校驗(yàn)。 "uniqueItems": True },????????"time":?{????????????"type":?"string",????????????#?取值為時(shí)間格式,需要先進(jìn)行轉(zhuǎn)換為format_checker參數(shù),值如:draft7_format_checker, 網(wǎng)址: # https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.Draft7Validator "format": "date", },????????"encoding":?{ "type": "string", # 符合該關(guān)鍵字指定的正則表達(dá)式,才算通過(guò)校驗(yàn)。 "pattern": "^[A-Z]$" },????????"otherInfo":?{ "type": "object", "properties": { "otherInfo1": { "type": "string" }, "otherInfo2": { "type": "string" } } } },????#?最少一級(jí)key?的個(gè)數(shù) "minProperties": 3,????#?最多一級(jí)key 的個(gè)數(shù)。 "maxProperties": 7,????#?patternProperties對(duì)象的每一個(gè)一級(jí)key都是一個(gè)正則表達(dá)式 # 下面的JSON Schema表示, 所有以a開頭的一級(jí)key的value都必須是int類型, "patternProperties": { "^a": { "type": "int" },????},????#?該關(guān)鍵字限制了JSON對(duì)象中必須包含哪些一級(jí)key。????"required":?["name",?"money"]}#?以上是定義的schema的校驗(yàn)規(guī)則,具體實(shí)際業(yè)務(wù)中可自行更改校驗(yàn)規(guī)則# 以下為待校驗(yàn)的響應(yīng)數(shù)據(jù)response=?{????"name":?"張三",????"money":?6666,????"tags":?["啦啦啦"],????"time":?"2020-6-20", "otherinfo1": { "otherinfo1": "1111",????????"otherinfo2":?"222" }}#?以下為校驗(yàn)代碼try: validate(instance=response, schema=schema, format_checker=draft7_format_checker)except SchemaError as e: print(f"驗(yàn)證模式schema出錯(cuò):\n出錯(cuò)位置:{'-->'.join([i for i in e.path])}\n提示信息:{e.message}")except ValidationError as e: print(f"json數(shù)據(jù)不符合schema規(guī)定:\n出錯(cuò)字段:{'-->'.join([i for i in e.path])}\n提示信息:{e.message}")else:????print("數(shù)據(jù)正確!")以上這些掌握對(duì)應(yīng)接口進(jìn)行斷言則足以使用,我們可根據(jù)自身業(yè)務(wù),設(shè)置響應(yīng)的校驗(yàn)規(guī)則,不許加那么多校驗(yàn),滿足接口指標(biāo)檢驗(yàn)即可。
總結(jié)
以上是生活随笔為你收集整理的json schema如何约束为小数_如何使用jsonschema进行接口响应断言的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python实现翻译功能_Python爬
- 下一篇: swing打地鼠游戏_【亲子早教】9月早