diff --git a/docs/basics.rst b/docs/basics.rst index 230ff94..7ddbc15 100644 --- a/docs/basics.rst +++ b/docs/basics.rst @@ -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 -------------