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.
Causes the validation chain to stop.
If StopValidation is raised, no more validators in the validation chain are called.
Raised when a validator fails to validate its input.
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.
Compares the values of two fields.
Parameters: |
|
---|
Compares the value to another value.
Parameters: |
|
---|
Adds conditions to the document-related queries.
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.
Validates an IP(v4) address.
Adds conditions to the document-related queries: the field must match the pattern.
Validates the length of a string.
Parameters: |
|
---|
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: |
|
---|
Adds conditions to the document-related queries.
alias of NumberRange
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.
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.
Validates the field against a user provided regexp.
Parameters: |
|
---|
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.
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: |
|
---|
Adds conditions to the document-related queries: the field must match the pattern.
Compares the incoming data to a sequence of valid inputs.
Parameters: |
|
---|
Adds conditions to the document-related queries.
Compares the incoming data to a sequence of invalid inputs.
Parameters: |
|
---|
Adds conditions to the document-related queries.