Added some docs about multiple preparers.

This commit is contained in:
Joe Dallago
2012-09-18 08:23:37 +00:00
parent b81f62e4f5
commit b070b00be2

View File

@@ -105,7 +105,7 @@ deserialization but before validation; it prepares a deserialized
value for validation. Examples would be to prepend schemes that may be
missing on url values or to filter html provided by a rich text
editor. A preparer is not called during serialization, only during
deserialization.
deserialization. You can also pass a Schema Node a list of preparers.
The *validator* of a schema node is called after deserialization and
preparation ; it makes sure the value matches a constraint. An example of
@@ -443,6 +443,23 @@ __ http://pypi.python.org/pypi/htmllaundry/
preparer=htmllaundry.sanitize,
validator=colander.Length(1))
You can even specify multiple preparers to be run in order by passing
a list of functions to the 'preparer' kwarg, like so:
.. code-block:: python
:linenos:
import colander
# removes whitespace, newlines, and tabs from the beginning/end of a string
strip_whitespace = lambda v: v.strip(' \t\n\r') if v is not None else v
# replaces multiple spaces with a single space
remove_multiple_spaces = lambda v: re.sub(' +', ' ', v)
class Page(colander.MappingSchema):
title = colander.SchemaNode(colander.String())
content = colander.SchemaNode(colander.String(),
preparer=[strip_whitespace, remove_multiple_spaces],
validator=colander.Length(1))
Serialization
-------------