4

I'm using Colander to validate request parameters for a Pyramid web server. For example:

class MySchema(colander.MappingSchema):                                         
    first_name = colander.SchemaNode(colander.String())                               
    last_name = colander.SchemaNode(colander.String())                              

Here, first_name and last_name are required parameters. If I use missing='' then this would make them optional, but they would still be added to the deserializing as an empty string which isn't really useful if the user submits an empty string.

Best I can think of is missing=None and then check for None later.

Is there a way to mark a parameter as truly optional? Meaning, if they're not in the request, they shouldn't be in the deserialized result either.

4

1 回答 1

4

我想你正在寻找missing=colander.drop.
来自文档

colander.drop - 表示如果在反序列化过程中丢失,将从模式中删除的值。作为值传递给 SchemaNode 的缺失关键字参数。

于 2015-09-22T12:51:48.763 回答