3

假设我有一个简单的架构:

class MySchema(colander.MappingSchema):
    thing = colander.SchemaNode(colander.Int())

使用上面的架构,在尝试反序列{'thing': None}化时出现错误:

Invalid: {'thing': u'Required'}

看起来滤锅处理具有None值的字段与缺少字段的方式相同。我怎样才能解决这个问题并强制执行thing总是提供的,但允许它存在None

4

3 回答 3

4

请考虑这个解决方案。

import colander


class NoneAcceptantNode(colander.SchemaNode):
    """Accepts None values for schema nodes.
    """

    def deserialize(self, value):
        if value is not None:
            return super(NoneAcceptantNode, self).deserialize(value)


class Person(colander.MappingSchema):
    interest = NoneAcceptantNode(colander.String())


# Passes
print Person().deserialize({'interest': None})

# Passes
print Person().deserialize({'interest': 'kabbalah'})

# Raises an exception
print Person().deserialize({})
于 2013-12-02T16:23:09.627 回答
2

None 值将用于反序列化,但是您需要在架构中提供“缺失”参数:

class MySchema(colander.MappingSchema):
    thing = colander.SchemaNode(colander.Int(), missing=None)

http://docs.pylonsproject.org/projects/colander/en/latest/null.html#deserializing-the-null-value

于 2013-09-13T00:12:00.553 回答
0

这是我正在使用的。我将空字符串映射到显式空值。如果 required 标志为真,则会引发无效错误。

from colander import SchemaNode as SchemaNodeNoNull

class _SchemaNode(SchemaNodeNoNull):

    nullable = True

    def __init__(self, *args, **kwargs):
        # if this node is required but nullable is not set, then nullable is
        # implicitly False
        if kwargs.get('missing') == required and kwargs.get('nullable') is None:
            kwargs['nullable'] = False
        super(_SchemaNode, self).__init__(*args, **kwargs)

    def deserialize(self, cstruct=null):
        if cstruct == '':
            if not self.nullable:
                raise Invalid(self, _('Cannot be null'))
            if self.validator:
                self.validator(self, cstruct)
            return None  # empty string means explicit NULL value
        ret = super(_SchemaNode, self).deserialize(cstruct)
        return ret

此外,在处理查询字符串参数时, foo=,bar= 将变为:

{
   "foo": "",
   "bar": ""
}

文字空值仅适用于 JSON 有效负载

于 2016-11-10T19:13:30.653 回答