在python前夕的子对象中使用`datetime`进行奇怪的验证行为

时间:2017-09-06 12:39:17

标签: python eve cerberus

我在测试我的python eve API时看到了一些非常奇怪的验证行为。

  • Eve 0.7.4
  • Mongod v3.2.10

简化摘要:我有一个域端点test,其架构包含一个对象props,该对象有两个子属性timewetBulb

DOMAIN = {

    # test end point
    'test': {
        'schema': {
            'props': {
                'type': 'dict',
                'schema': { 
                    'time':     {'type': 'datetime'},           # datetime fails to validate
                    'wetBulb':  {'type': ['string', 'float']} # only see issue while key is camel case and contains two type options
                }
            }
        },
        'resource_methods': ['GET', 'POST']
    }
}

DATE_FORMAT = '%Y-%m-%d %H:%M:%S'   # date format seems to be irrelevant

当我将wetBulb定义为驼峰大小写时(即wetBulb而不是wet_bulb)和/或将其定义为两种潜在类型之一(即'type': ['string', 'float']) ,eve / cerberus无法将time值解释为datetime

实施例

使用所有其他默认值在本地运行,我使用requests库测试此API点:

import requests 

post = {
    'properties': {
        'time': '2017-09-05 20:17:40',
        'wetBulb': '21.200000000000003' 
    }
}

r = requests.post('http://127.0.0.1:5000/test', data=json.dumps(post), headers={'content-type': 'application/json'})

# print response
print('Status {0}'.format(r.status_code))
print(r.text)

我得到了回复

Status 422
{"_status": "ERR", "_issues": {"properties": {"time": "must be of datetime type"}}, "_error": {"message": "Insertion failure: 1 document(s) contain(s) error(s)", "code": 422}}

如果我更改模式定义以使密钥wet_bulb和/或我将其类型更改为'type': 'string',则会正确验证:

DOMAIN = {

    # test end point
    'test': {
        'schema': {
            'props': {
                'type': 'dict',
                'schema': { 
                    'time':     {'type': 'datetime'},       
                    'wet_bulb': {'type': 'string'}
                }
            }
        },
        'resource_methods': ['GET', 'POST']
    }
}

然后使用更新的对象发布对象:

post = {
    'properties': {
        'time': '2017-09-05 20:17:40',
        'wet_bulb': '21.200000000000003' 
    }
}

r = requests.post('http://127.0.0.1:5000/test', data=json.dumps(post), headers={'content-type': 'application/json'})

# print response
print('Status {0}'.format(r.status_code))
print(r.text)

Status 201
{"_updated": "2017-09-06 12:30:18", "_links": {"self": {"href": "vos/59afea5aa8a548256898cc40", "title": "Vo"}}, "_created": "2017-09-06 12:30:18", "_status": "OK", "_id": "59afea5aa8a548256898cc40", "_etag": "f1b918a2fe688941f84f4d00bc1b400abddab446"}

问题:

有没有人看到过这样的其他行为,或者可以帮助澄清可能控制此验证行为的内容?我已经进行了探索,试图确定导致验证的原因但尚未成功。

修改

我认为部分解释位于cerberus文档中type下面的两个注释中,但我仍然没有找到原因:

http://docs.python-cerberus.org/en/stable/validation-rules.html#type

我在不同架构中遇到相同的datetime验证问题。在这种情况下,我通过删除'type'声明或将'type'设置为字符串而不是列表(即'type': 'number'而不是'type': ['number', 'list']来处理它。不幸的是这个模式我还没有为申请工作,所以我暂时删除了'type'声明。

0 个答案:

没有答案