Validators

A validator simply takes an input and verifies it fulfills some criterion, such as a maximum length for a string. If the validation fails, a ValidationError is raised. This simple system allows chaining any number of validators on fields.

The module is heavily inspired by (and partially ripped off from) the WTForms validators. However, ours serve a bit different purpose. First, error messages are not needed here (the errors will not be displayed to end users). Second, these validators include query filtering capabilities.

Usage example:

class Person(Document):
    validators = {
        'first_name': [required(), length(min=2)],
        'age': [number_range(min=18)],
    }

This document will raise ValidationError if you attempt to save it with wrong values. You can call Document.is_valid() to ensure everything is OK.

Now let’s query the database for all objects of Person:

Person.objects(db)

Doqu does not deal with tables or collections, it follows the DRY (Don’t Repeat Yourself) principle and uses the same validators to determine what database records belong to given document class. The schema defined above is alone equivalent to the following query:

...where(first_name__exists=True, age__gte=18).where_not(first_name='')

This is actually the base query available as Person.objects(db).

Note

not all validators affect document-related queries. See detailed documentation on each validator.

exception doqu.validators.StopValidation

Causes the validation chain to stop.

If StopValidation is raised, no more validators in the validation chain are called.

exception doqu.validators.ValidationError

Raised when a validator fails to validate its input.

class doqu.validators.Email

Validates an email address. Note that this uses a very primitive regular expression and should only be used in instances where you later verify by other means, such as email activation or lookups.

Adds conditions to the document-related queries: the field must match the pattern.

doqu.validators.email

alias of Email

class doqu.validators.EqualTo(name)

Compares the values of two fields.

Parameters:
  • name – The name of the other field to compare to.
doqu.validators.equal_to

alias of EqualTo

class doqu.validators.Equals(other_value)

Compares the value to another value.

Parameters:
  • other_value – The other value to compare to.

Adds conditions to the document-related queries.

doqu.validators.equals

alias of Equals

class doqu.validators.Exists

Ensures given field exists in the record. This does not affect validation of a document with pre-defined structure but does affect queries.

Adds conditions to the document-related queries.

doqu.validators.exists

alias of Exists

class doqu.validators.IPAddress

Validates an IP(v4) address.

Adds conditions to the document-related queries: the field must match the pattern.

doqu.validators.ip_address

alias of IPAddress

class doqu.validators.Length(min=None, max=None)

Validates the length of a string.

Parameters:
  • min – The minimum required length of the string. If not provided, minimum length will not be checked.
  • max – The maximum length of the string. If not provided, maximum length will not be checked.
doqu.validators.length

alias of Length

class doqu.validators.NumberRange(min=None, max=None)

Validates that a number is of a minimum and/or maximum value, inclusive. This will work with any comparable number type, such as floats and decimals, not just integers.

Parameters:
  • min – The minimum required value of the number. If not provided, minimum value will not be checked.
  • max – The maximum value of the number. If not provided, maximum value will not be checked.

Adds conditions to the document-related queries.

doqu.validators.number_range

alias of NumberRange

class doqu.validators.Optional

Allows empty value (i.e. bool(value) == False) and terminates the validation chain for this field (i.e. no more validators are applied to it). Note that errors raised prior to this validator are not suppressed.

doqu.validators.optional

alias of Optional

class doqu.validators.Required

Requires that the value is not empty, i.e. bool(value) returns True. The bool values can also be False (but not anything else).

Adds conditions to the document-related queries: the field must exist and be not equal to an empty string.

doqu.validators.required

alias of Required

class doqu.validators.Regexp(pattern, flags=0)

Validates the field against a user provided regexp.

Parameters:
  • regex – The regular expression string to use.
  • flags – The regexp flags to use, for example re.IGNORECASE or re.UNICODE.

Note

the pattern must be provided as string because compiled patterns cannot be used in database lookups.

Adds conditions to the document-related queries: the field must match the pattern.

doqu.validators.regexp

alias of Regexp

class doqu.validators.URL(require_tld=True)

Simple regexp based url validation. Much like the email validator, you probably want to validate the url later by other means if the url must resolve.

Parameters:
  • require_tld – If true, then the domain-name portion of the URL must contain a .tld suffix. Set this to false if you want to allow domains like localhost.

Adds conditions to the document-related queries: the field must match the pattern.

doqu.validators.url

alias of URL

class doqu.validators.AnyOf(choices)

Compares the incoming data to a sequence of valid inputs.

Parameters:
  • choices – A sequence of valid inputs.

Adds conditions to the document-related queries.

doqu.validators.any_of

alias of AnyOf

class doqu.validators.NoneOf(choices)

Compares the incoming data to a sequence of invalid inputs.

Parameters:
  • choices – A sequence of invalid inputs.

Adds conditions to the document-related queries.

doqu.validators.none_of

alias of NoneOf

Previous topic

Glossary

Next topic

Utilities

This Page