API Reference¶
Data¶
-
lollipop.types.
MISSING
= <MISSING>¶ Special singleton value (like None) to represent case when value is missing.
Types¶
-
class
lollipop.types.
Type
(validate=None, *args, **kwargs)[source]¶ Base class for defining data types.
Parameters: validate (list) – A validator or list of validators for this data type. Validator is a callable that takes serialized data and raises ValidationError
if data is invalid. Validator return value is ignored.-
dump
(value, context=None)[source]¶ Serialize data to primitive types. Raises
ValidationError
if data is invalid.Parameters: - value – Value to serialize.
- context – Context data.
-
load
(data, context=None)[source]¶ Deserialize data from primitive types. Raises
ValidationError
if data is invalid.Parameters: - data – Data to deserialize.
- context – Context data.
-
-
class
lollipop.types.
Any
(validate=None, *args, **kwargs)[source]¶ Any type. Does not transform/validate given data.
-
class
lollipop.types.
Integer
(validate=None, *args, **kwargs)[source]¶ An integer type.
-
num_type
¶ alias of
int
-
-
class
lollipop.types.
Float
(validate=None, *args, **kwargs)[source]¶ A float type.
-
num_type
¶ alias of
float
-
-
class
lollipop.types.
List
(item_type, **kwargs)[source]¶ A homogenous list type.
Example:
List(String()).load(['foo', 'bar', 'baz'])
Parameters:
-
class
lollipop.types.
Tuple
(item_types, **kwargs)[source]¶ A heterogenous list type.
Example:
Tuple([String(), Integer(), Boolean()]).load(['foo', 123, False])
Parameters: - item_types (list) – List of item types.
- kwargs – Same keyword arguments as for
Type
.
-
class
lollipop.types.
Dict
(value_types=<Any>, **kwargs)[source]¶ A dict type. You can specify either a single type for all dict values or provide a dict-like mapping object that will return proper Type instance for each given dict key.
Example:
Dict(Integer()).load({'key0': 1, 'key1': 5, 'key2': 15}) Dict({'foo': String(), 'bar': Integer()}).load({ 'foo': 'hello', 'bar': 123, })
Parameters:
-
class
lollipop.types.
OneOf
(types, load_hint=<function type_name_hint>, dump_hint=<function type_name_hint>, *args, **kwargs)[source]¶ Example:
class Foo(object): def __init__(self, foo): self.foo = foo class Bar(object): def __init__(self, bar): self.bar = bar FooType = Object({'foo': String()}, constructor=Foo) BarType = Object({'bar': Integer()}, constructor=Bar) def object_with_type(name, subject_type): return Object(subject_type, {'type': name}, constructor=subject_type.constructor) FooBarType = OneOf({ 'Foo': object_with_type('Foo', FooType), 'Bar': object_with_type('Bar', BarType), }, dump_hint=type_name_hint, load_hint=dict_value_hint('type')) List(FooBarType).dump([Foo(foo='hello'), Bar(bar=123)]) # => [{'type': 'Foo', 'foo': 'hello'}, {'type': 'Bar', 'bar': 123}] List(FooBarType).load([{'type': 'Foo', 'foo': 'hello'}, {'type': 'Bar', 'bar': 123}]) # => [Foo(foo='hello'), Bar(bar=123)]
-
lollipop.types.
type_name_hint
(data)[source]¶ Returns type name of given value.
To be used as a type hint in
OneOf
.
-
lollipop.types.
dict_value_hint
(key, mapper=None)[source]¶ Returns a function that takes a dictionary and returns value of particular key. The returned value can be optionally processed by
mapper
function.To be used as a type hint in
OneOf
.
-
class
lollipop.types.
Field
(field_type, *args, **kwargs)[source]¶ Base class for describing
Object
fields. Defines a way to access object fields during serialization/deserialization. Usually it extracts data to serialize/deserialize and callself.field_type.load()
to do data transformation.Parameters: field_type (Type) – Field type. -
dump
(name, obj, *args, **kwargs)[source]¶ Serialize data to primitive types. Raises
ValidationError
if data is invalid.Parameters: - name (str) – Name of attribute to serialize.
- obj – Application object to extract serialized value from.
-
load
(name, data, *args, **kwargs)[source]¶ Deserialize data from primitive types. Raises
ValidationError
if data is invalid.Parameters: - name (str) – Name of attribute to deserialize.
- data – Raw data to get value to deserialize from.
-
-
class
lollipop.types.
ConstantField
(field_type, value, *args, **kwargs)[source]¶ Field that always serializes to given value and does not deserialize.
Parameters: - field_type (Type) – Field type.
- value – Value constant for this field.
-
class
lollipop.types.
AttributeField
(field_type, attribute=None, *args, **kwargs)[source]¶ Field that corresponds to object attribute.
Parameters: - field_type (Type) – Field type.
- attribute (str) – Use given attribute name instead of field name defined in object type.
-
class
lollipop.types.
MethodField
(field_type, method, *args, **kwargs)[source]¶ Field that is result of method invocation.
Example:
class Person(object): def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name def get_name(self): return self.first_name + ' ' + self.last_name PersonType = Object({ 'name': MethodField(String(), 'get_name'), }, constructor=Person)
Parameters: - field_type (Type) – Field type.
- method (str) – Method name. Method should not take any arguments.
-
class
lollipop.types.
FunctionField
(field_type, function, *args, **kwargs)[source]¶ Field that is result of function invocation.
Example:
class Person(object): def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name def get_name(person): return person.first_name + ' ' + person.last_name PersonType = Object({ 'name': FunctionField(String(), get_name), }, constructor=Person)
Parameters: - field_type (Type) – Field type.
- function (callable) – Function that takes source object and returns field value.
-
class
lollipop.types.
Object
(bases_or_fields=None, fields=None, constructor=<type 'dict'>, default_field_type=<class 'lollipop.types.AttributeField'>, allow_extra_fields=True, only=None, exclude=None, **kwargs)[source]¶ An object type. Serializes to a dict of field names to serialized field values. Parametrized with field names to types mapping. The way values are obtained during serialization is determined by type of field object in
fields
mapping (seeConstantField
,AttributeField
,MethodField
for details). You can specify eitherField
object, aType
object or any other value. In case ofType
, it will be automatically wrapped with a default field type, which is controlled bydefault_field_type
constructor argument. In case of any other value it will be transformed intoConstantField
.Example:
class Person(object): def __init__(self, name, age): self.name = name self.age = age PersonType = Object({ 'name': String(), 'age': Integer(), }, constructor=Person) PersonType.load({'name': 'John', 'age': 42}) # => Person(name='John', age=42)
Parameters: - base_or_fields – Either
Object
instance or fields (Seefields
argument). In case of fields, the actual fields argument should not be specified. - fields – List of name-to-value tuples or mapping of object field names to
Type
,Field
objects or constant values. - contructor (callable) – Deserialized value constructor. Constructor should take all fields values as keyword arguments.
- default_field_type (Field) – Default field type to use for fields defined by their type.
- allow_extra_fields (bool) – If False, it will raise
ValidationError
for all extra dict keys during deserialization. If True, will ignore all extra fields. - only (list) – List of field names to include in this object. All other fields (own or inherited) won’t be used.
- exclude (list) – List of field names to exclude from this object. All other fields (own or inherited) will be included.
- kwargs – Same keyword arguments as for
Type
.
- base_or_fields – Either
-
class
lollipop.types.
Optional
(inner_type, load_default=None, dump_default=None, **kwargs)[source]¶ A wrapper type which makes values optional: if value is missing or None, it will not transform it with an inner type but instead will return None (or any other configured value).
Example:
UserType = Object({ 'email': String(), # by default types require valid values 'name': Optional(String()), # value can be omitted or None 'role': Optional( # when value is omitted or None, use given value String(validate=AnyOf(['admin', 'customer'])), load_default='customer', ), })
Parameters:
Validators¶
-
class
lollipop.validators.
Validator
(error_messages=None, *args, **kwargs)[source]¶ Base class for all validators.
Validator is used by types to validate data during deserialization. Validator class should define
__call__
method with either one or two arguments. In both cases, first argument is value being validated. In case of two arguments, the second one is the context. If given value fails validation,__call__
method should raiseValidationError
. Return value is always ignored.
-
class
lollipop.validators.
Predicate
(predicate, error=None, **kwargs)[source]¶ Validator that succeeds if given predicate returns True.
Parameters: - predicate (callable) – Predicate that takes value and returns True or False. One- and two-argument predicates are supported. First argument in both cases is value being validated. In case of two arguments, the second one is context.
- error (str) – Error message in case of validation error.
Can be interpolated with
data
.
-
class
lollipop.validators.
Range
(min=None, max=None, **kwargs)[source]¶ Validator that checks value is in given range.
Parameters: - min (int) – Minimum length. If not provided, minimum won’t be checked.
- max (int) – Maximum length. If not provided, maximum won’t be checked.
- error (str) – Error message in case of validation error.
Can be interpolated with
data
,min
ormax
.
-
class
lollipop.validators.
Length
(exact=None, min=None, max=None, **kwargs)[source]¶ Validator that checks value length (using
len()
) to be in given range.Parameters: - exact (int) – Exact length. If provided,
min
andmax
are not checked. If not provided,min
andmax
checks are performed. - min (int) – Minimum length. If not provided, minimum length won’t be checked.
- max (int) – Maximum length. If not provided, maximum length won’t be checked.
- error (str) – Error message in case of validation error.
Can be interpolated with
data
,length
,exact
,min
ormax
.
- exact (int) – Exact length. If provided,
-
class
lollipop.validators.
NoneOf
(values, error=None, **kwargs)[source]¶ Validator that succeeds if
value
is not a member of givenvalues
.Parameters: - values (iterable) – A sequence of invalid values.
- error (str) – Error message in case of validation error.
Can be interpolated with
data
andvalues
.
-
class
lollipop.validators.
AnyOf
(choices, error=None, **kwargs)[source]¶ Validator that succeeds if
value
is a member of givenchoices
.Parameters: - choices (iterable) – A sequence of allowed values.
- error (str) – Error message in case of validation error.
Can be interpolated with
data
andchoices
.
-
class
lollipop.validators.
Regexp
(regexp, flags=0, error=None, **kwargs)[source]¶ Validator that succeeds if
value
matches givenregex
.Parameters: - regexp (str) – Regular expression string.
- flags (int) – Regular expression flags, e.g. re.IGNORECASE. Not used if regexp is not a string.
- error (str) – Error message in case of validation error.
Can be interpolated with
data
andregexp
.
Errors¶
-
lollipop.errors.
SCHEMA
= '_schema'¶ Name of an error key for cases when you have both errors for the object and for it’s fields:
{'field1': 'Field error', '_schema': 'Whole object error'}
-
exception
lollipop.errors.
ValidationError
(messages)[source]¶ Exception to report validation errors.
Examples of valid error messages:
raise ValidationError('Error') raise ValidationError(['Error 1', 'Error 2']) raise ValidationError({ 'field1': 'Error 1', 'field2': {'subfield1': ['Error 2', 'Error 3']} })
Parameters: messages – Validation error messages. String, list of strings or dict where keys are nested fields and values are error messages.
-
class
lollipop.errors.
ValidationErrorBuilder
[source]¶ Helper class to report multiple errors.
Example:
def validate_all(data): builder = ValidationErrorBuilder() if data['foo']['bar'] >= data['baz']['bam']: builder.add_error('foo.bar', 'Should be less than bam') if data['foo']['quux'] >= data['baz']['bam']: builder.add_fields('foo.quux', 'Should be less than bam') ... builder.raise_errors()
-
add_error
(path, error)[source]¶ Add error message for given field path.
Example:
builder = ValidationErrorBuilder() builder.add_error('foo.bar.baz', 'Some error') print builder.errors # => {'foo': {'bar': {'baz': 'Some error'}}}
Parameters: - path (str) – ‘.’-separated list of field names
- error (str) – Error message
-
add_errors
(errors)[source]¶ Add errors in dict format.
Example:
builder = ValidationErrorBuilder() builder.add_errors({'foo': {'bar': 'Error 1'}}) builder.add_errors({'foo': {'baz': 'Error 2'}, 'bam': 'Error 3'}) print builder.errors # => {'foo': {'bar': 'Error 1', 'baz': 'Error 2'}, 'bam': 'Error 3'}
Parameters: list or dict errors (str,) – Errors to merge
-
raise_errors
()[source]¶ Raise
ValidationError
if errors are not empty; do nothing otherwise.
-
-
lollipop.errors.
merge_errors
(errors1, errors2)[source]¶ Deeply merges two error messages. Error messages can be string, list of strings or dict of error messages (recursively). Format is the same as accepted by
ValidationError
. Returns new error messages.