zoukankan      html  css  js  c++  java
  • jsonschema数据类型校验

    pip install jsonschema

    from jsonschema import validate
    
    '''
    _DEPRECATED_DEFAULT_TYPES = {
        u"array": list,
        u"boolean": bool,
        u"integer": int,
        u"null": type(None),
        u"number": numbers.Number,
        u"object": dict,
        u"string": str,
    }
    '''
    
    schema = {
        "type": "object",
        "properties": {
            "price": {"type": "number"},
            "nameisnull": {"type": "null"},
            "bool_test": {"type": "boolean"},
            "int_test": {"type": "integer"},
            "array_test": {"type": "array"},
            "string_test": {"type": "string"}
        },
    }
    
    validate(instance={"nameisnull": None, "price": 34.99, "bool_test": True, "int_test": 1, "array_test": [1, 2, 3, 4, 5],
                       "type": {}, "string_test": "abc"}, schema=schema)

    以上校验的话是不会报错的,如果在稍微修改数据类型就会报错

    schema = {
        "type": "object",
        "properties": {
            "price": {"type": "number"},
            "nameisnull": {"type": "null"},
            "bool_test": {"type": "boolean"},
            "int_test": {"type": "integer"},
            "array_test": {"type": "array"},
            "string_test": {"type": "string"}
        },
    }
    
    validate(instance={"nameisnull": "hhh", "price": 34.99, "bool_test": True, "int_test": 1, "array_test": [1, 2, 3, 4, 5],
                       "type": {}, "string_test": "abc"}, schema=schema)

  • 相关阅读:
    opencv for java via cmake-gui
    ubuntu的设置里少了好多设置
    vue组件化编程应用2
    vue组件化编程应用
    vue组件化编程
    es6基本介绍及使用
    webpack基本介绍及使用
    npm基本介绍及使用
    node基本介绍及使用
    前后端分离基本介绍
  • 原文地址:https://www.cnblogs.com/RainBol/p/14037452.html
Copyright © 2011-2022 走看看