Colander: how do I allow None values?

3.2k views Asked by At

Say I have a simple schema:

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

With the schema above, when trying to deserialize {'thing': None} I get the error:

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

It looks like colander treats fields with a None value the same way as missing fields. How can I get around that and enforce that thing is always provided, but allow it to be None?

3

There are 3 answers

5
tim-phillips On

A None value will work for deserialization, however you need to supply a 'missing' argument in your schema:

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

0
JamesHutchison On

Here's what I'm using. I have the empty string mapped to an explicit null value. If the required flag is true, then it raises an invalid error.

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

Also, when dealing with querystring parameters, foo=,bar= will become:

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

A literal null value is only possible with JSON payloads

0
negus On

Please consider this solution.

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({})