Validators

The validators module contains a set of fields validators.

A validator is any object with a validate() method which receives a value as the target for the validation. If the validation fails then the validate() method should raise an errors.ValidationError exception with an error message.

Validators are passed to fields.Field and subclasses as possitional arguments.

validators.nullable(method)[source]

This is a helper validation decorator for validators that allow their values to be None.

The String validator is a good example:

class String(object):
    def validate(self, value):
        if value is not None:
            pass # Do the validation here ...

Now the same but using the @nullable decorator:

@nullable
def validate(self, value):
    pass # Do the validation here ...
class validators.Required[source]

This validator forces fields to have a value other than None.

class validators.In(choices)[source]

This validator forces fields to have their value in the given list.

Parameters:choices – A list of possible values.
class validators.String[source]

This validator forces fields values to be an instance of basestring.

class validators.Integer[source]

This validator forces fields values to be an instance of int.

class validators.Float[source]

This validator forces fields values to be an instance of float.

class validators.Boolean[source]

This validator forces fields values to be an instance of bool.

class validators.Model(model)[source]

This validator forces fields values to be an instance of the given models.Model subclass and also performs a validation in the entire model object.

Parameters:model – A subclass of models.Model
class validators.Email[source]

This validator forces fields values to be strings and match a valid email address.

class validators.List(*validators)[source]

This validator forces field values to be a list. Also a list of inner validators could be specified to validate each list element. For example, to validate a list of models.Model you could do:

books = fields.Field(validators.List(validators.Model(YourBookModel)))
Parameters:*validators – A list of inner validators as possitional arguments.

Project Versions

Previous topic

Fields

Next topic

Errors

This Page