學習了JsonSchema,我自定義了一個校驗代碼

JsonSchema

使用fastjsonschema來校驗數據


# 導入驗證器
import json
import fastjsonschema

# 讀取schema
with open('../schema/oneof-schema.json', encoding='utf8') as f:
    my_schema = json.load(f)

# json數據:
with open('../data/test.json', encoding='utf8') as f:
    json_data = json.load(f)

# 驗證:
fastjsonschema.validate(my_schema, json_data)

使用jsonschema來校驗數據


import json
# 導入驗證器
from jsonschema import validate, draft7_format_checker, SchemaError, ValidationError

if __name__ == '__main__':
    with open('../schema/MySchema.json', encoding='utf8') as f:
        my_schema = json.load(f)

    # json數據:
    with open('../data/cece.json', encoding='utf8') as f:
        json_data = json.load(f)

    # error_list = check_type(my_schema, json_data)
    # print(error_list)

    # 驗證:
    try:
        validate(instance=json_data, schema=my_schema, format_checker=draft7_format_checker)
        # Draft7Validator.format_checker
    except SchemaError as serr:
        print("schema 錯誤 【%s】 \nschema錯誤" % str(serr))
    except ValidationError as verr:
        print("數據 錯誤 【%s】 \n數據錯誤" % str(verr))

MySchema

JSONSchema缺點

  1. 錯誤提示英文
  2. 校驗數據爲一步步校驗,遇到錯誤停止

自定義JSONSchema

schema遵循 http://json-schema.org/

“$schema”: “http://json-schema.org/draft-07/schema#”,

使用方法 >>>> 點擊這裏

代碼

個人編寫的校驗的代碼,自定義成分較多
目前僅僅擴展了 string類型的數據 format 的選型判斷

CheckDataUti.py


import re
import time

# email 正則表達式
EMAIL_REGEX = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
# URL 正則表達式
URL_REGEX = "^[a-zA-z]+://[^\s]*$"
# PHONE 正則表達式
PHONE_REGEX = "^([1][3,4,5,6,7,8,9])\d{9}$"
# 身份證 正則表達式
ID_CARD_REGEX = "^((\d{18})|([0-9x]{18})|([0-9X]{18}))$"
# 郵政編碼 正則表達式
ZIP_CODE_REGEX = "^[1-9]\d{5}(?!\d)$"
# IP 地址 正則表達式
IP_REGEX = "^\d+\.\d+\.\d+\.\d+$"
# 正整數
INTEGER_REGEX = "^[1-9]\d*$"

ERR_LIST = []
COMMON_ERR_LIST = []


def log_error(msg, data, schema, is_common=False):
    """
    打印錯誤日誌
    """
    err_log = "%s,數據:【%s】,校驗規則: %s" % (str(msg), str(data) + " type of " + str(type(data).__name__), str(schema))

    if not is_common:
        ERR_LIST.append(err_log)
        print("=================================================")
        print(err_log)
        print("=================================================")
    else:
        COMMON_ERR_LIST.append(err_log)


def check_object(data, schema, is_common):
    """
    校驗對象格式
    【 properties、required、minProperties、maxProperties、patternProperties、additionalProperties 】
    """
    if type(data) != dict:
        log_error("當前校驗的json不是一個對象格式", data, schema, is_common)
    else:
        # 獲取當前校驗數據的所有key
        keys = dict.keys(data)

        # 處理必需值
        if "required" in schema:
            required_schema = schema['required']
            for schema_key in required_schema:
                if schema_key not in keys:
                    log_error("字段【%s】必填" % schema_key, data, schema, is_common)

        # 處理最小key和最大key
        if "minProperties" in schema:
            min_properties = schema['minProperties']
            if len(keys) < min_properties:
                log_error("校驗數據的key個數小於【%s】" % str(min_properties), data, schema, is_common)

        if "maxProperties" in schema:
            max_properties = schema['maxProperties']
            if len(keys) > max_properties:
                log_error("校驗數據的key個數大於【%s】" % str(max_properties), data, schema, is_common)

        # 處理具體的key
        if "properties" in schema:
            # 處理 properties
            properties_schema = schema['properties']
            schema_keys = dict.keys(properties_schema)
            for data_key in schema_keys:
                if data_key in data:
                    check_data(properties_schema[data_key], data[data_key])

        # 處理滿足正則表達式的key
        if "patternProperties" in schema:
            # 處理 properties
            pattern_properties = schema['patternProperties']
            schema_keys = dict.keys(pattern_properties)

            # 循環所有正則表達式的key
            for schema_key in schema_keys:
                # 循環當前待校驗的數據key
                for data_key in keys:
                    # 僅僅處理滿足正則表達式的key數據
                    if re.match(schema_key, data_key):
                        check_data(pattern_properties[schema_key], data[data_key])


def check_array(data, schema, is_common):
    """
    校驗數組格式
    【 items、additionalItems、minItems、maxItems、uniqueItems 】
    """
    if type(data) != list:
        log_error("當前校驗的json不是數組格式", data, schema, is_common)
    else:
        # minItems、maxItems
        # 判斷最小值
        if "minItems" in schema:
            min_items = schema['minItems']
            if len(data) < min_items:
                log_error("當前校驗的數據數組長度小於【%s】" % str(min_items), data, schema, is_common)

        # 判斷最大值
        if "maxItems" in schema:
            max_properties = schema['maxItems']
            if len(data) > max_properties:
                log_error("當前校驗的數據數組長度大於【%s】" % str(max_properties), data, schema, is_common)

        # uniqueItems true 數組元素不能重複
        if "uniqueItems" in schema:
            unique_items_schema = schema['uniqueItems']

            if unique_items_schema:
                # 數組元素不能重複
                try:
                    if len(set(data)) != len(data):
                        log_error("當前校驗的數據數組元素不能重複", data, schema, is_common)
                except TypeError:
                    # 存在數組內部元素是dict格式
                    pass
        # 判斷每一個items
        if "items" in schema:
            items_schema = schema["items"]
            # 判斷items_schema 是數組還是對象
            if type(items_schema) is list:
                # 如果是數組 每一個item都是一個jsonSchema 索引對應的數組內索引的格式
                index = 0
                for item_sc in items_schema:
                    check_data(item_sc, data[index])
                    index += 1

                # additionalItems該關鍵字只有在items是數組的時候纔會有效
                # additionalItems 除了上述規定之外的數據必需符合指定的規則
                if "additionalItems" in schema:
                    additional_items_schema = schema['additionalItems']

                    for i in range(index, len(data)):
                        check_data(additional_items_schema, data[i])

            # items如果是對象  當前schema規範了數組內所有元素的格式
            elif type(items_schema) is dict:
                for item_data in data:
                    check_data(items_schema, item_data)


def check_number(data, schema, is_common):
    """
    校驗數字類型
    """
    if type(data) not in (int, float):
        log_error("當前校驗的json不是一個數字格式", data, schema, is_common)
    else:
        # 判斷最大值 maximum 如果exclusiveMaximum該關鍵字是True 包含本身
        if "maximum" in schema:
            maximum_schema = schema['maximum']
            if 'exclusiveMaximum' in schema and schema['exclusiveMaximum']:
                if data >= maximum_schema:
                    log_error("當前校驗的數據大於等於【%s】" % maximum_schema, data, schema, is_common)
            else:
                if data > maximum_schema:
                    log_error("當前校驗的數據大於【%s】" % maximum_schema, data, schema, is_common)

        # minimum、exclusiveMinimum
        if "minimum" in schema:
            minimum_schema = schema['minimum']
            if 'exclusiveMinimum' in schema and schema['exclusiveMinimum']:
                if data <= minimum_schema:
                    log_error("當前校驗的數據小於等於【%s】" % minimum_schema, data, schema, is_common)
            else:
                if data < minimum_schema:
                    log_error("當前校驗的數據小於【%s】" % minimum_schema, data, schema, is_common)

        # multipleOf    整除
        if "multipleOf" in schema:
            multiple_of_schema = schema['multipleOf']
            if not data % multiple_of_schema == 0:
                log_error("當前校驗的數據不能被%s整除" % multiple_of_schema, data, schema, is_common)


def check_str(data, schema, is_common):
    """
    校驗字符串類型
    涉及的關鍵字 【maxLength、minLength、pattern、format】
    """
    if type(data) != str:
        log_error("當前校驗的數據不是一個字符串格式", data, schema, is_common)
    else:
        # maxLength
        if "maxLength" in schema:
            max_length_schema = schema['maxLength']
            if len(data) > max_length_schema:
                log_error("當前校驗的數據長度大於%d" % max_length_schema, data, schema, is_common)

        # minLength
        if "minLength" in schema:
            min_length_schema = schema['minLength']
            if len(data) < min_length_schema:
                log_error("當前校驗的數據長度小於%d" % min_length_schema, data, schema, is_common)

        # pattern
        if "pattern" in schema:
            pattern_schema = schema['pattern']
            if not re.match(pattern_schema, data):
                log_error("當前校驗的數據不符合正則表達式規則【%s】" % pattern_schema, data, schema, is_common)
        # format
        if 'format' in schema:
            format_schema = schema['format']

            if format_schema == 'email' and not re.match(EMAIL_REGEX, data):
                log_error("當前校驗的數據不是正確的郵箱格式", data, schema, is_common)

            elif format_schema == 'phone' and not re.match(PHONE_REGEX, data):
                log_error("當前校驗的數據不是正確的手機號碼格式", data, schema, is_common)

            elif format_schema == 'hostname' and not re.match(IP_REGEX, data):
                log_error("當前校驗的數據不是正確的IP地址格式", data, schema, is_common)

            elif format_schema == 'idCard' and not re.match(ID_CARD_REGEX, data):
                log_error("當前校驗的數據不是正確的身份證格式", data, schema, is_common)

            elif format_schema == 'date':
                format_patten = '%Y-%m-%d'
                if 'format_patten' in schema:
                    format_patten = schema['format_patten']
                try:
                    time.strptime(data, format_patten)
                except ValueError:
                    log_error("當前校驗的數據不是正確的日期格式格式【%s】" % format_patten, data, schema, is_common)


def check_common(schema, data):
    """
    校驗通用的
    涉及到關鍵字:
    【 enum、const、allOf、anyOf、oneOf、not、 if……then…… 】
    """
    if "enum" in schema:
        enum_schema = schema['enum']
        if data not in enum_schema:
            log_error("當前校驗的數據值不存在【%s】中" % str(enum_schema), data, schema)

    if "const" in schema:
        const_schema = schema['const']
        if data != const_schema:
            log_error("當前校驗數據值不等於【%s】" % str(const_schema), data, schema)

    if "allOf" in schema:
        all_of_schema = schema['allOf']
        for item_schema in all_of_schema:
            check_data(item_schema, data)
    if "anyOf" in schema:
        any_of_schema = schema['anyOf']

        begin_len = len(COMMON_ERR_LIST)

        for item_schema in any_of_schema:
            check_data(item_schema, data, True)

        end_len = len(COMMON_ERR_LIST)

        if end_len - begin_len == len(any_of_schema):
            log_error("當前校驗的數據不符合當前anyof中的任一規則", data, schema)

    if "oneOf" in schema:
        one_of_schema = schema['oneOf']

        begin_len = len(COMMON_ERR_LIST)

        for item_schema in one_of_schema:
            check_data(item_schema, data, True)

        end_len = len(COMMON_ERR_LIST)

        if end_len - begin_len != len(one_of_schema) - 1:
            log_error("待校驗JSON元素不能通過oneOf的校驗", data, schema)

    if "not" in schema:
        not_schema = schema['not']
        begin_len = len(COMMON_ERR_LIST)
        check_data(not_schema, data, True)
        end_len = len(COMMON_ERR_LIST)

        if end_len == begin_len:
            log_error("待校驗JSON元素不能通過not規則的校驗", data, schema)

    # if……then……
    if 'if' in schema:
        if_schmea = schema['if']
        begin_len = len(COMMON_ERR_LIST)
        check_data(if_schmea, data, True)
        end_len = len(COMMON_ERR_LIST)

        if end_len == begin_len:
            if "then" in schema:
                then_schema = schema['then']
                check_data(then_schema, data, False)
        else:
            if "else" in schema:
                else_schema = schema['else']
                check_data(else_schema, data, False)

def get_data_type(data):
    """
    獲取type
    """
    if type(data) == dict:
        return 'object'
    if type(data) == list:
        return 'array'
    if type(data) in (int, float):
        return 'number'
    if type(data) == str:
        return 'string'
    if type(data) == bool:
        return 'boolean'


def check_data(schema, data, is_common=False):
    # 優先處理 通用的
    check_common(schema, data)

    # 沒有type的情況
    # type 默認爲string
    type_name = schema['type'] if "type" in schema else get_data_type(data)

    if type_name == 'object':
        check_object(data, schema, is_common)
    elif type_name == 'array':
        check_array(data, schema, is_common)
    elif type_name in ['integer', 'number']:
        check_number(data, schema, is_common)
    elif type_name == 'string':
        check_str(data, schema, is_common)
    # type是布爾類型
    elif type_name == 'boolean':
        if type(data) != bool:
            log_error("當前校驗的數據不是一個boolean格式", data, schema, is_common)


JsonSchmea 的數據示例


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "minProperties": 1,
  "maxProperties": 200,
  "properties": {
    "name": {
      "type": "string",
      "enum": [
        "shaofei",
        "upuptop",
        "pyfysf"
      ]
    },
    "email": {
      "type": "string",
      "format": "email",
      "const": "[email protected]"
    },
    "idCard": {
      "type": "string",
      "format": "idCard",
      "pattern": "\\d+"
    },
    "phone": {
      "type": "string",
      "format": "phone"
    },
    "hostname": {
      "type": "string",
      "format": "hostname"
    },
    "createTime": {
      "format": "date",
      "format_patten": "%Y%m%d"
    },
    "is": {
      "type": "boolean"
    },
    "age": {
      "type": "integer",
      "maximum": 20,
      "minimum": 1,
      "multipleOf": 2
    },
    "like": {
      "type": "array"
    }
  },
  "allOf": [
    {
      "type": "string"
    }
  ],
  "patternProperties": {
    "^\\S+123$": {
      "type": "integer"
    }
  },
  "required": [
    "email"
  ]
}

使用方式


import json

from CheckDataUti import check_data

if __name__ == '__main__':
    with open('../schema/MySchema.json', encoding='utf8') as f:
        my_schema = json.load(f)

    # json數據:
    with open('../data/cece.json', encoding='utf8') as f:
        json_data = json.load(f)

    check_data(my_schema, json_data)
    # print(ERR_LIST)

參考:

schema遵循 http://json-schema.org/

“$schema”: “http://json-schema.org/draft-07/schema#”,

使用方法 >>>> 點擊這裏

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章