python 接口参数校验

# 范围校验
class params_check:
    """
    @param: 校验参数 {名称,值}
    @param: 校验参数取值点 [0,1,2]
    @param:校验参数类型  String、Int
    对单个参数进行校验,返回值为  Boolean, String
    """

    def __init__(self):
        pass

    def check_param_range_type(self, param, param_range, param_type):
        for k, v in param.items():
            # 范围校验
            range_check_result, range_check_info = self.check_range(k, v, param_range)
            if not range_check_result:
                return range_check_result, range_check_info
            # 类型校验
            type_check_result, type_check_info = self.check_type(k, v, param_type)
            if not type_check_result:
                return type_check_result, type_check_info
        return True, "check success"

    def check_param_range(self, param, param_range):
        for k, v in param:
            # 范围校验
            range_check_result, range_check_info = self.check_range(k, v, param_range)
            if not range_check_result:
                return range_check_result, range_check_info
        return True, "check success"

    def check_param_type(self, param, param_type):
        for k, v in param:
            # 类型校验
            type_check_result, type_check_info = self.check_type(k, v, param_type)
            if not type_check_result:
                return type_check_result, type_check_info
        return True, "check success"

    def check_range(self, key, value, param_range):
        # 范围校验
        if value not in param_range:
            return False, "Input parameter value error, [" + key + "] should be in the" + str(param_range)
        else:
            return True, "check range success"

    def check_type(self, key, value, param_type):
        # 类型校验
        if type(value).__name__.lower() != param_type.lower():
            return False, "Input parameter type error, [" + key + "] should be " + param_type
        else:
            return True, "check type success"

 

posted @ 2023-05-08 13:39  wangssd  阅读(426)  评论(0编辑  收藏  举报