python开发接口时,使用jsonschema模块对数据进行校验

2022/2/7 11:12:59

本文主要是介绍python开发接口时,使用jsonschema模块对数据进行校验,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import jsonschema

schema = {
    "type": "object",  # 先声明每个键都是对象
    "properties": {  # 声明每个键对应的值的类型
        "version_no": {"type": "string"},
        "versions": {
            "type": "array", # 如果键对应的值是列表数据类型,则需要声明类型为array,然后再声明列表里面的键对应的值
            "minItems": 1,
            "items": {
                "type": "object",  # 字典对象
                "required": ["name", "type"], # 必填字段
                "properties": {  # 声明键对应的值的类型约束等
                    "name": {  
                        "type": "string",
                        "minLength": 1
                    },
                    "type": {
                        "type": "string",
                        "enum": ["python", "java"]
                    },
                    "size": {
                        "type": "number",  # 整型对应的是integer
                    },
                }
            }
        },
    },
    "required": ["version_no", "versions"]  # 最外围的键的约束
}


data = {
    "version_no": "x123",
    "versions": [
        {
            "name": "jack",
            "type": "python",
            "size": 123
        },
        {
            "name": "will",
            "type": "java",
        }
    ]
}


def schema_check(data, schema):
    try:
        jsonschema.validate(date, schema)
        print("data中的数据通过自定义的json schema校验")
    except jsonschema.exceptions.ValidationError as e;
    	print(e.message, "data数据没有通过schema格式校验")
        
        
if __name__ == '__main__':
    schema_check(data, schema)
    
    

# 装饰器版
from jsonschema import ValidationError, validate
from .exceptions import InvalidJSON

def json_schema_validate(schema, is_get=False):
    def validate_func(func):
        def _func(self, request, *args, **kwargs):
            try:
                if not is_get:
                    validate(request.data, schema)
                else:
                    validate(request.query_params, schema)
            except ValidationError as e:
                logger.exception('Invalid jsonschema')
                raise InvalidJSON(e) from e
            else:
                return func(self, request, *args, **kwargs)
        return _func
    return validate_func


@json_schema_validate({
    "type": "object",  # 先声明每个键都是对象
    "properties": {  # 声明每个键对应的值的类型
        "version_no": {"type": "string"},
        "versions": {
            "type": "array", # 如果键对应的值是列表数据类型,则需要声明类型为array,然后再声明列表里面的键对应的值
            "minItems": 1,
            "items": {
                "type": "object",  # 字典对象
                "required": ["name", "type"], # 必填字段
                "properties": {  # 声明键对应的值的类型约束等
                    "name": {  
                        "type": "string",
                        "minLength": 1
                    },
                    "type": {
                        "type": "string",
                        "enum": ["python", "java"]
                    },
                    "size": {
                        "type": "number",  # 整型对应的是integer
                    },
                }
            }
        },
    },
    "required": ["version_no", "versions"]  # 最外围的键的约束
})
def post(self, request):
    ....


这篇关于python开发接口时,使用jsonschema模块对数据进行校验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程