Use these structures to build up a serializers.

Every structure returns an object that has two methods. serialize returns objects ready to be encoded into JSON, or other formats. deserialize will validate and return objects ready to be used internally, or it will raise a validation excepton.

class strainer.structure.Translator(serialize, deserialize)

Translator is an internal data structure that holds a reference to a serialize and deserialize function. All structures return a translator.

strainer.structure.child(source_field, target_field=None, serializer=None, validators=None, attr_getter=None, full_validators=None)

A child is a nested serializer.

strainer.structure.dict_field(*args, **kwargs)

dict_field is just like field except that it pulls attributes out of a dict, instead of off an object.

strainer.structure.field(source_field, target_field=None, validators=None, attr_getter=None, formatters=None)

Constructs an indvidual field for a serializer, this is on the order of one key, and one value.

The field determines the mapping between keys internaly, and externally. As well as the proper validation at the level of the field.

>>> from collections import namedtuple
>>> Aonly = namedtuple('Aonly', 'a')
>>> model = Aonly('b')
>>> one_field = field('a')
>>> one_field.deserialize(model)
{'a': 'b'}
  • source_field (str) – What attribute to get from a source object
  • target_field (str) – What attribute to place the value on the target, optional. If optional target is equal to source_field
  • validators (list) – A list of validators that will be applied during deserialization.
  • formaters (list) – A list of formaters that will be applied during serialization.
  • attr_getter (function) – Overrides the default method for getting the soure_field off of an object
strainer.structure.many(source_field, target_field=None, serializer=None, validators=None, attr_getter=None, full_validators=None)

Many allows you to nest a list of serializers


This function creates a serializer from a list fo fields


Validators are functions that validate data.

strainer.validators.boolean(*args, **kwargs)

Converts a field into a boolean

strainer.validators.datetime(*args, **kwargs)

validates that a a field is an ISO 8601 string, and converts it to a datetime object.

strainer.validators.integer(*args, **kwargs)

converts a value to integer, applying optional bounds

strainer.validators.required(*args, **kwargs)

validates that a field exists in the input

strainer.validators.string(*args, **kwargs)

converts a value into a string, optionally with a max length


Formatters are functions that transform data.

strainer.formatters.format_datetime(*args, **kwargs)

Formats a value as an iso8601 datetime


This is just a set of utilities to help take a deserialized dict and turn it into JSON. It handles things like datetime objects.

exception strainer.exceptions.ValidationException(errors)

This exception keeps track of all the exceptions thrown during validations