日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

json schema如何约束为小数_如何使用jsonschema进行接口响应断言

發布時間:2024/4/19 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 json schema如何约束为小数_如何使用jsonschema进行接口响应断言 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一,JSONSchema的概念

JSONSchema是一種用來描述JSON數據的一種JSON數據結構。JSON Schema也有版本,目前的版本有 Draft 7,Draft 6,Draft 4 和Draft 3。使用之前我們先進行cmd安裝:pip install jsonschema

二,JSONSchema解析

2.1,type節點

  • string

  • number(int/float)

  • object(dict)

  • array(list)

  • boolean

  • null

{ "type": "string" }

2.2,properties

json以key--value的形式存在,實際就是描述存在的key值。

"properties":?{"name":?{"type":?"string"}

2.3,propertyNames

實際是用來描述properties中key的信息,可用正則匹配進行限制描述。

"properties":?{"name_123":?{"type":?"string","propertyNames":"^[A-Za-z_][A-Za-z0-9_]*$"}

以上采用了key的名稱進行限制,必須以字母開頭,且以"_"鏈接字母或字符的形式,否則將不符合規范。

2.4,required

required是用來描述json中必須存在哪些key。

data= { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "number"},????"gender":?{"type":?"string"}?? },????"required":?["name"]???#?要求檢測d的json數據必須包含name這個key??}

2.5,enum

enum枚舉值限制,意思是json數據某個key的value值,只能是以下這幾個枚舉里的值。

"name":?{"type":?"string","enum":["張三","李四","王五"]}

三,schema 實例

schema = { # 該關鍵字用于指定JSON Schema版本: draft-07????"$schema":?"http://json-schema.org/draft-07/schema#",????#?描述對應的JSON元素????"description":?"這是一個比較齊全的schema校驗",????#?該關鍵字用于限定待校驗JSON元素所屬的數據類型????"type":?"object", "properties": { "name_id": { "description": "這是第一個一級key", "type": "integer", # 類型為數字????????????"minimum":?1???#?限制最小值為1 }, "name": {????????????"description":?"第二個一級key",????????????"type":?"string",????#?類型為字符串 "minLength": 3, # 最小長度為3????????????"maxLength":?10????#?最大長度為10 }, "class_name": {????????????"description":?"一級key用來描述班級名稱", "type": "string",????????????"minLength":?4,????????????"maxLength":?10 }, "skill": { "anyOf": [ # 滿足其中一個類型 就行????????????????{"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 } ], # 待校驗JSON數組第一個元素是string類型,且可接受的最短長度為5個字符,第二個元素是number類型,且可接受的最小值為10 # 剩余的其他元素是string類型,且可接受的最短長度為2。 "additonalItems": { "type": "string", "miniLength": 2 }, # 至少一個 "miniItems": 1, # 最多5個 "maxItems": 5, # 值為true時,所有元素都具有唯一性時,才能通過校驗。 "uniqueItems": True },????????"time":?{????????????"type":?"string",????????????#?取值為時間格式,需要先進行轉換為format_checker參數,值如:draft7_format_checker, 網址: # https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.Draft7Validator "format": "date", },????????"encoding":?{ "type": "string", # 符合該關鍵字指定的正則表達式,才算通過校驗。 "pattern": "^[A-Z]$" },????????"otherInfo":?{ "type": "object", "properties": { "otherInfo1": { "type": "string" }, "otherInfo2": { "type": "string" } } } },????#?最少一級key?的個數 "minProperties": 3,????#?最多一級key 的個數。 "maxProperties": 7,????#?patternProperties對象的每一個一級key都是一個正則表達式 # 下面的JSON Schema表示, 所有以a開頭的一級key的value都必須是int類型, "patternProperties": { "^a": { "type": "int" },????},????#?該關鍵字限制了JSON對象中必須包含哪些一級key。????"required":?["name",?"money"]}#?以上是定義的schema的校驗規則,具體實際業務中可自行更改校驗規則# 以下為待校驗的響應數據response=?{????"name":?"張三",????"money":?6666,????"tags":?["啦啦啦"],????"time":?"2020-6-20", "otherinfo1": { "otherinfo1": "1111",????????"otherinfo2":?"222" }}#?以下為校驗代碼try: validate(instance=response, schema=schema, format_checker=draft7_format_checker)except SchemaError as e: print(f"驗證模式schema出錯:\n出錯位置:{'-->'.join([i for i in e.path])}\n提示信息:{e.message}")except ValidationError as e: print(f"json數據不符合schema規定:\n出錯字段:{'-->'.join([i for i in e.path])}\n提示信息:{e.message}")else:????print("數據正確!")

以上這些掌握對應接口進行斷言則足以使用,我們可根據自身業務,設置響應的校驗規則,不許加那么多校驗,滿足接口指標檢驗即可。

總結

以上是生活随笔為你收集整理的json schema如何约束为小数_如何使用jsonschema进行接口响应断言的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。