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)

  • 相关阅读:
    进程与线程的区别
    信号列表详解
    同步与互斥
    互斥锁
    读写锁
    Redis QPS测试
    从分布式锁来看redis和zookpeer!
    JVM虚拟机调参
    log4j.properties配置详解与实例
    生产者消费者(消费者要消费完才能退出)
  • 原文地址:https://www.cnblogs.com/RainBol/p/14037452.html
Copyright © 2011-2022 走看看