Skip to main content

A JSON contract tool that outputs build matches

Project description

说明: 主要用于json数据diff,输出匹配的字段信息、字段匹配准确率

安装方式: shell script pip install verify_json

1、 生成契约校验实例

from json_verify import JsonVerify

demo_json = {
    "msg": "一段文字信息",
    "phone": 13746067836,
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18}
    ]
}
check_json = {
    "msg": None,
    "address": "上海",
    "name": [
        {"age": 12, "language": "chinese"},
        {"phone": 213213, "age": 18},
    ]
}
instance = JsonVerify(demo_json)
instance.verify(check_json)
print(instance.info)
>>>
{"loseKey": ["phone"], "increaseKey": ["address", "name.0.language"], "keyError": {"msg": {"type_rules": ["str"], "check_value": null, "detail": "数据类型错误"}}, "patchRate": "66.67%"}

备注:校验规则{key:value}

value:为列表类型时默认校验列表元素,每个元素与锲约列表匹配程度,与元素在列表中顺序无关、与元素长短无关

默认契约校验为值数据类型作为校验规则

  • loseKey: 被检验json数据缺少的key

  • increaseKey:被检验json增加的key

  • keyError:被检验json的key对应value不符合锲约要求

  • patchRate:所有key匹配的正确率(increaseKey不参与运算)

2、自定义契约

自定义契约字段需要加上 下划线 前后缀

锲约json
{"name": "百里清"}

自定义锲约时:
{"_name_": {"rules_value": ["百里清"]}}

2.1 自定义值匹配契约 rules_value

from json_verify import JsonVerify

demo_json = {
    "_msg_": {"rules_value": ["不匹配的文字信息"]},
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18}
    ]
}
check_json = {
    "msg": "错误信息",
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18},
    ]
}
instance = JsonVerify(demo_json)
instance.verify(check_json)
print(instance.info)
>>>
{"loseKey": [], "increaseKey": [], "keyError": {"msg": {"rules_value": ["不匹配的文字信息"], "check_value": "错误信息", "detail": "期望值与结果值不同"}}, "patchRate": "80.00%"}

2.2 自定义正则匹配契约 regular_rules

from json_verify import JsonVerify

demo_json = {
    "_msg_": {"regular_rules": "^\d+$"},
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18}
    ]
}
check_json = {
    "msg": "错误信息",
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18},
    ]
}
instance = JsonVerify(demo_json)
instance.verify(check_json)
print(instance.info)
>>>
{"loseKey": [], "increaseKey": [], "keyError": {"msg": {"regular_rules": "^\\d+$", "check_value": "错误信息", "detail": "正则匹配错误"}}, "patchRate": "80.00%"}

2.3 自定义函数契约 func_rules

from json_verify import JsonVerify

demo_json = {
    "_msg_": {"func_rules": "check_msg"}, # func_rules: 函数要存在于check_func对象里
    "name": [
        {"_age_": {"func_rules": "check_age"}},
        {"phone": 213213, "_age_": {"func_rules": "check_age"}}
    ]
}
check_json = {
    "msg": "错误信息",
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18},
    ]
}


class CheckFunction:

    def check_msg(self, msg):
        if msg:
            return True

    def check_age(self, age):

        return True if age>12 else False

instance = JsonVerify(demo_json, check_func=CheckFunction())
# check_func: 校验函数存放的包、模块、类实例 可以使用getattr(check_func, func_name) 获取的类型
instance.verify(check_json)

print(instance.info)
>>>
{"loseKey": [], "increaseKey": [], "keyError": {"name.0.age": {"func_rules": "check_age", "check_value": 12, "detail": "值不符合函数校验规则"}}, "patchRate": "80.00%"}

3、 做精确值匹配 check_value_only

注意: 启用此方法时任何自定义契约校验都会失效

from json_verify import JsonVerify

demo_json = {
    "msg": "123", # func_rules: 函数要存在于check_func对象里
    "name": [
        {"age": 18},
        {"phone": 213213, "age": 18}
    ]
}
check_json = {
    "msg": "错误信息",
    "name": [
        {"age": 12},
        {"phone": 213213, "age": 18},
    ]
}


class CheckFunction:

    def check_msg(self, msg):
        if msg:
            return True

    def check_age(self, age):
        return True if age>12 else False

instance = JsonVerify(demo_json, check_func=CheckFunction(), check_value_only=True)
# check_func: 校验函数存放的包、模块、类实例 可以使用getattr(check_func, func_name) 获取的类型
instance.verify(check_json)

print(instance.info)
>>>
{"loseKey": [], "increaseKey": [], "keyError": {"name": {"rules_value": [[{"age": 18}, {"phone": 213213, "age": 18}]], "check_value": [{"age": 12}, {"phone": 213213, "age": 18}], "detail": "期望值与结果值不同"}, "name.0": {"rules_value": [{"age": 18}, {"phone": 213213, "age": 18}], "check_value": {"age": 12}, "detail": "期望值与结果值不同"}, "name.0.age": {"rules_value": [18], "check_value": 12, "detail": "期望值与结果值不同"}}, "patchRate": "40.00%"}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

verify_json-1.0.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

verify_json-1.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file verify_json-1.0.2.tar.gz.

File metadata

  • Download URL: verify_json-1.0.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.7

File hashes

Hashes for verify_json-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8bf9567bdc675df2100c888825a660d7e467186bdccaeaf7191d0071b39172ff
MD5 9598fdc431604659ac1b2b8e919a86dc
BLAKE2b-256 5dc0a8600d2126f7523f4267f99b8850defbc1ef227b124f6bb0283207965092

See more details on using hashes here.

File details

Details for the file verify_json-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: verify_json-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.7

File hashes

Hashes for verify_json-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8a7c630bb7dd12aa03b53858ff0c6e3cfb754bf73b989701f11ebff7fe26c972
MD5 0012a2d5fc47db6e23c6e00100fec2c6
BLAKE2b-256 7cd6578a6e793f4bfea1c931420d3cf4ed1b27bfc94f95a373a79c2e459c0105

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page