#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/4/22 10:15 # @Author : ZhangJianPing # @Site : # @File : check_params.py # @Software: PyCharm from jsonschema import validate, ValidationError def check_param(param_dict, json_schema): """ :param param_dict: 待校验字符串 :param json_schema: 校验格式 :return: """ try: validate(param_dict, json_schema) except Exception as e: print('error: ', e) raise e def check_json_param(param_dict): """ :param param_dict: 待校验字符串 :return: """ schema = { 'type': 'object', 'properties': { 'id': {'type': 'integer'}, 'title': {'type': 'string'}, 'content': {'type': 'string'}, 'memo': {'type': 'string'} }, 'required': ['id', 'title', 'content'] } check_param(param_dict, schema) # 校验参数之后,具体业务逻辑 return if __name__ == '__main__': param_dict = { 'id': '1', # 字符串会报错, 'title': 'schema校验标题', 'content': 'schema校验内容', 'memo': 'schema校验备注信息' } check_json_param(param_dict)
校验通过,什么都不操作,校验失败,抛异常