【问题标题】:Jsonschema xxx is not of type objectJsonschema xxx 不是对象类型
【发布时间】:2019-11-22 10:06:13
【问题描述】:

我使用 python 和 json 模式作为数据验证工具,下面是我的模式:

    ValidatorSchema = {
        "type": "object",
        "required": ["data"],
        "properties": {
            "data":
                {
                    "type": "object",
                    "required": ["account", "password", "clientType"],
                    "properties": {
                        "account": {
                            "type": "string"
                        },
                        "password": {
                            "type": "string"
                        },
                        "clientType": {
                            "type": "integer",
                            "enum": [ClientTypeEnum.USER_EMAIL.code, ClientTypeEnum.USER_MOBILE.code,
                                     ClientTypeEnum.USER_MINA.code,
                                     ClientTypeEnum.USER_WECHAT.code]
                        }
                    }
                }
        }
    }

还有我的数据:

{
    "data": {
        "account": "ccl",
        "password": "12345678",
        "level": 0,
        "client_type": 100
    }
}

但验证器总是向我显示错误,我该如何解决它:

SchemaError: b'{\n    "data": {\n        "account": "ccl",\n        "password": "12345678",\n        "level": 0,\n        "client_type": 100\n    }\n}' is not of type 'object'

【问题讨论】:

  • 您是否尝试将数据作为字符串传递给验证函数?我们可以看看您导入 JSON 并将其传递给函数的代码吗?
  • 使用jsononpickle.decode(request.data)处理数据后,现在可以了。

标签: python-3.x jsonschema


【解决方案1】:

您的架构表明以下字段是必需的:

["account", "password", "clientType"]

但是,你的 json 有:

{
    "account": "ccl",
    "password": "12345678",
    "client_type": 100 
}

你能看出区别吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-02
    • 2021-08-29
    • 1970-01-01
    • 2020-04-18
    • 2023-03-22
    • 2019-12-01
    • 2016-11-26
    • 2012-01-23
    相关资源
    最近更新 更多