This commit is contained in:
@@ -439,17 +439,17 @@ but allows some wiggle room for ``t``, ``on``, ``yes``, ``y``, and
|
|||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
class Boolean(object):
|
class Boolean(object):
|
||||||
def deserialize(self, struct, value):
|
def deserialize(self, node, value):
|
||||||
if not isinstance(value, basestring):
|
if not isinstance(value, basestring):
|
||||||
raise Invalid(struct, '%r is not a string' % value)
|
raise Invalid(node, '%r is not a string' % value)
|
||||||
value = value.lower()
|
value = value.lower()
|
||||||
if value in ('true', 'yes', 'y', 'on', 't', '1'):
|
if value in ('true', 'yes', 'y', 'on', 't', '1'):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def serialize(self, struct, value):
|
def serialize(self, node, value):
|
||||||
if not isinstance(value, bool):
|
if not isinstance(value, bool):
|
||||||
raise Invalid(struct, '%r is not a boolean')
|
raise Invalid(node, '%r is not a boolean')
|
||||||
return value and 'true' or 'false'
|
return value and 'true' or 'false'
|
||||||
|
|
||||||
Here's how you would use the resulting class as part of a schema:
|
Here's how you would use the resulting class as part of a schema:
|
||||||
@@ -470,6 +470,13 @@ Note that the only real constraint of a type class is that its
|
|||||||
``serialize`` method must be able to make sense of a value generated
|
``serialize`` method must be able to make sense of a value generated
|
||||||
by its ``deserialize`` method and vice versa.
|
by its ``deserialize`` method and vice versa.
|
||||||
|
|
||||||
|
The serialize and deserialize methods of a type accept two values:
|
||||||
|
``node``, and ``value``. ``node`` will be the schema node associated
|
||||||
|
with this type. It is used when the type must raise a
|
||||||
|
:exc:`colander.Invalid` error, which expects a schema node as its
|
||||||
|
first constructor argument. ``value`` will be the value that needs to
|
||||||
|
be serialized or deserialized.
|
||||||
|
|
||||||
For a more formal definition of a the interface of a type, see
|
For a more formal definition of a the interface of a type, see
|
||||||
:class:`colander.interfaces.Type`.
|
:class:`colander.interfaces.Type`.
|
||||||
|
|
||||||
@@ -477,7 +484,7 @@ Defining a New Validator
|
|||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
A validator is a callable which accepts two positional arguments:
|
A validator is a callable which accepts two positional arguments:
|
||||||
``struct`` and ``value``. It returns ``None`` if the value is valid.
|
``node`` and ``value``. It returns ``None`` if the value is valid.
|
||||||
It raises a :class:`colander.Invalid` exception if the value is not
|
It raises a :class:`colander.Invalid` exception if the value is not
|
||||||
valid. Here's a validator that checks if the value is a valid credit
|
valid. Here's a validator that checks if the value is a valid credit
|
||||||
card number.
|
card number.
|
||||||
@@ -485,7 +492,7 @@ card number.
|
|||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
def luhnok(struct, value):
|
def luhnok(node, value):
|
||||||
""" checks to make sure that the value passes a luhn mod-10 checksum """
|
""" checks to make sure that the value passes a luhn mod-10 checksum """
|
||||||
sum = 0
|
sum = 0
|
||||||
num_digits = len(value)
|
num_digits = len(value)
|
||||||
@@ -502,7 +509,7 @@ card number.
|
|||||||
sum = sum + digit
|
sum = sum + digit
|
||||||
|
|
||||||
if not (sum % 10) == 0:
|
if not (sum % 10) == 0:
|
||||||
raise Invalid(struct,
|
raise Invalid(node,
|
||||||
'%r is not a valid credit card number' % value)
|
'%r is not a valid credit card number' % value)
|
||||||
|
|
||||||
Here's how the resulting ``luhnok`` validator might be used in a
|
Here's how the resulting ``luhnok`` validator might be used in a
|
||||||
@@ -521,6 +528,10 @@ string: this has already been done as the result of the type of the
|
|||||||
``cc_number`` schema node being :class:`colander.String`. Validators
|
``cc_number`` schema node being :class:`colander.String`. Validators
|
||||||
are always passed the *deserialized* value when they are invoked.
|
are always passed the *deserialized* value when they are invoked.
|
||||||
|
|
||||||
|
The ``node`` value passed to the validator is a schema node object; it
|
||||||
|
must in turn be passed to the :exc:`colander.Invalid` exception
|
||||||
|
constructor if one needs to be raised.
|
||||||
|
|
||||||
For a more formal definition of a the interface of a validator, see
|
For a more formal definition of a the interface of a validator, see
|
||||||
:class:`colander.interfaces.Validator`.
|
:class:`colander.interfaces.Validator`.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user