Validation Rules¶
allow_unknown¶
This can be used in conjunction with the schema rule
when validating a mapping in order to set the
allow_unknown
property of the validator for the
subdocument.
This rule has precedence over purge_unknown
(see Purging Unknown Fields).
For a full elaboration refer to this paragraph.
allowed¶
This rule takes a collections.abc.Container
of allowed values.
Validates the target value if the value is in the allowed values.
If the target value is an iterable, all its members must be in the
allowed values.
>>> v.schema = {'role': {'type': 'list', 'allowed': ['agent', 'client', 'supplier']}}
>>> v.validate({'role': ['agent', 'supplier']})
True
>>> v.validate({'role': ['intern']})
False
>>> v.errors
{'role': ["unallowed values ('intern',)"]}
>>> v.schema = {'role': {'type': 'string', 'allowed': ['agent', 'client', 'supplier']}}
>>> v.validate({'role': 'supplier'})
True
>>> v.validate({'role': 'intern'})
False
>>> v.errors
{'role': ['unallowed value intern']}
>>> v.schema = {'a_restricted_integer': {'type': 'integer', 'allowed': [-1, 0, 1]}}
>>> v.validate({'a_restricted_integer': -1})
True
>>> v.validate({'a_restricted_integer': 2})
False
>>> v.errors
{'a_restricted_integer': ['unallowed value 2']}
Changed in version 0.5.1: Added support for the int
type.
allof¶
Validates if all of the provided constraints validates the field. See *of-rules for details.
New in version 0.9.
anyof¶
Validates if any of the provided constraints validates the field. See *of-rules for details.
New in version 0.9.
check_with¶
Validates the value of a field by calling either a function or method.
A function must be implemented like the following prototype:
def functionnname(field, value, error):
if value is invalid:
error(field, 'error message')
The error
argument points to the calling validator’s _error
method. See
Extending Cerberus on how to submit errors.
Here’s an example that tests whether an integer is odd or not:
def oddity(field, value, error):
if not value & 1:
error(field, "Must be an odd number")
Then, you can validate a value like this:
>>> schema = {'amount': {'check_with': oddity}}
>>> v = Validator(schema)
>>> v.validate({'amount': 10})
False
>>> v.errors
{'amount': ['Must be an odd number']}
>>> v.validate({'amount': 9})
True
If the rule’s constraint is a string, the Validator
instance
must have a method with that name prefixed by _check_with_
. See
Methods that can be referenced by the check_with rule for an equivalent to the function-based example
above.
The constraint can also be a sequence of these that will be called consecutively.
schema = {'field': {'check_with': (oddity, 'prime number')}}
Changed in version 1.3: The rule was renamed from validator
to check_with
contains¶
This rule validates that the a container object contains all of the defined items.
>>> document = {'states': ['peace', 'love', 'inity']}
>>> schema = {'states': {'contains': 'peace'}}
>>> v.validate(document, schema)
True
>>> schema = {'states': {'contains': 'greed'}}
>>> v.validate(document, schema)
False
>>> schema = {'states': {'contains': ['love', 'inity']}}
>>> v.validate(document, schema)
True
>>> schema = {'states': {'contains': ['love', 'respect']}}
>>> v.validate(document, schema)
False
dependencies¶
This rule allows one to define either a single field name, a sequence of field names or a mapping of field names and a sequence of allowed values as required in the document if the field defined upon is present in the document.
>>> schema = {'field1': {'required': False}, 'field2': {'required': False, 'dependencies': 'field1'}}
>>> document = {'field1': 7}
>>> v.validate(document, schema)
True
>>> document = {'field2': 7}
>>> v.validate(document, schema)
False
>>> v.errors
{'field2': ["field 'field1' is required"]}
When multiple field names are defined as dependencies, all of these must be present in order for the target field to be validated.
>>> schema = {'field1': {'required': False}, 'field2': {'required': False},
... 'field3': {'required': False, 'dependencies': ['field1', 'field2']}}
>>> document = {'field1': 7, 'field2': 11, 'field3': 13}
>>> v.validate(document, schema)
True
>>> document = {'field2': 11, 'field3': 13}
>>> v.validate(document, schema)
False
>>> v.errors
{'field3': ["field 'field1' is required"]}
When a mapping is provided, not only all dependencies must be present, but also any of their allowed values must be matched.
>>> schema = {'field1': {'required': False},
... 'field2': {'required': True, 'dependencies': {'field1': ['one', 'two']}}}
>>> document = {'field1': 'one', 'field2': 7}
>>> v.validate(document, schema)
True
>>> document = {'field1': 'three', 'field2': 7}
>>> v.validate(document, schema)
False
>>> v.errors
{'field2': ["depends on these values: {'field1': ['one', 'two']}"]}
>>> # same as using a dependencies list
>>> document = {'field2': 7}
>>> v.validate(document, schema)
False
>>> v.errors
{'field2': ["depends on these values: {'field1': ['one', 'two']}"]}
>>> # one can also pass a single dependency value
>>> schema = {'field1': {'required': False}, 'field2': {'dependencies': {'field1': 'one'}}}
>>> document = {'field1': 'one', 'field2': 7}
>>> v.validate(document, schema)
True
>>> document = {'field1': 'two', 'field2': 7}
>>> v.validate(document, schema)
False
>>> v.errors
{'field2': ["depends on these values: {'field1': 'one'}"]}
Declaring dependencies on subdocument fields with dot-notation is also supported:
>>> schema = {
... 'test_field': {'dependencies': ['a_dict.foo', 'a_dict.bar']},
... 'a_dict': {
... 'type': 'dict',
... 'schema': {
... 'foo': {'type': 'string'},
... 'bar': {'type': 'string'}
... }
... }
... }
>>> document = {'test_field': 'foobar', 'a_dict': {'foo': 'foo'}}
>>> v.validate(document, schema)
False
>>> v.errors
{'test_field': ["field 'a_dict.bar' is required"]}
When a subdocument is processed the lookup for a field in question starts at
the level of that document. In order to address the processed document as
root level, the declaration has to start with a ^
. An occurrence of two
initial carets (^^
) is interpreted as a literal, single ^
with no
special meaning.
>>> schema = {
... 'test_field': {},
... 'a_dict': {
... 'type': 'dict',
... 'schema': {
... 'foo': {'type': 'string'},
... 'bar': {'type': 'string', 'dependencies': '^test_field'}
... }
... }
... }
>>> document = {'a_dict': {'bar': 'bar'}}
>>> v.validate(document, schema)
False
>>> v.errors
{'a_dict': [{'bar': ["field '^test_field' is required"]}]}
Note
If you want to extend semantics of the dot-notation, you can
override the _lookup_field()
method.
Note
The evaluation of this rule does not consider any constraints defined with the required rule.
Changed in version 1.0.2: Support for absolute addressing with ^
.
Changed in version 0.8.1: Support for sub-document fields as dependencies.
Changed in version 0.8: Support for dependencies as a dictionary.
New in version 0.7.
empty¶
If constrained with False
validation of an iterable value will fail
if it is empty.
Per default the emptiness of a field isn’t checked and is therefore allowed
when the rule isn’t defined. But defining it with the constraint True
will
skip the possibly defined rules allowed
, forbidden
, items
,
minlength
, maxlength
, regex
and validator
for that field when
the value is considered empty.
>>> schema = {'name': {'type': 'string', 'empty': False}}
>>> document = {'name': ''}
>>> v.validate(document, schema)
False
>>> v.errors
{'name': ['empty values not allowed']}
New in version 0.0.3.
excludes¶
You can declare fields to excludes others:
>>> v = Validator()
>>> schema = {'this_field': {'type': 'dict',
... 'excludes': 'that_field'},
... 'that_field': {'type': 'dict',
... 'excludes': 'this_field'}}
>>> v.validate({'this_field': {}, 'that_field': {}}, schema)
False
>>> v.validate({'this_field': {}}, schema)
True
>>> v.validate({'that_field': {}}, schema)
True
>>> v.validate({}, schema)
True
You can require both field to build an exclusive or:
>>> v = Validator()
>>> schema = {'this_field': {'type': 'dict',
... 'excludes': 'that_field',
... 'required': True},
... 'that_field': {'type': 'dict',
... 'excludes': 'this_field',
... 'required': True}}
>>> v.validate({'this_field': {}, 'that_field': {}}, schema)
False
>>> v.validate({'this_field': {}}, schema)
True
>>> v.validate({'that_field': {}}, schema)
True
>>> v.validate({}, schema)
False
You can also pass multiples fields to exclude in a list :
>>> schema = {'this_field': {'type': 'dict',
... 'excludes': ['that_field', 'bazo_field']},
... 'that_field': {'type': 'dict',
... 'excludes': 'this_field'},
... 'bazo_field': {'type': 'dict'}}
>>> v.validate({'this_field': {}, 'bazo_field': {}}, schema)
False
forbidden¶
Opposite to allowed this validates if a value is any but one of the defined values:
>>> schema = {'user': {'forbidden': ['root', 'admin']}}
>>> document = {'user': 'root'}
>>> v.validate(document, schema)
False
New in version 1.0.
items¶
Validates the items of any iterable against a sequence of rules that must validate each index-correspondent item. The items will only be evaluated if the given iterable’s size matches the definition’s. This also applies during normalization and items of a value are not normalized when the lengths mismatch.
>>> schema = {'list_of_values': {
... 'type': 'list',
... 'items': [{'type': 'string'}, {'type': 'integer'}]}
... }
>>> document = {'list_of_values': ['hello', 100]}
>>> v.validate(document, schema)
True
>>> document = {'list_of_values': [100, 'hello']}
>>> v.validate(document, schema)
False
See itemsrules rule for dealing with arbitrary length list
types.
itemsrules¶
All items of the term:sequence will be validated against the rules provided in the constraint.
>>> schema = {'a_list':
... {'type': 'list',
... 'itemsrules': {'type': 'integer'}}
... }
>>> document = {'a_list': [3, 4, 5]}
>>> v.validate(document, schema)
True
keysrules¶
This rules takes a set of rules as constraint that all keys of a mapping are validated with.
>>> schema = {'a_dict': {
... 'type': 'dict',
... 'keysrules': {'type': 'string', 'regex': '[a-z]+'}}
... }
>>> document = {'a_dict': {'key': 'value'}}
>>> v.validate(document, schema)
True
>>> document = {'a_dict': {'KEY': 'value'}}
>>> v.validate(document, schema)
False
New in version 0.9.
Changed in version 1.0: Renamed from propertyschema
to keyschema
Changed in version 1.3: Renamed from keyschema
to keysrules
meta¶
This is actually not a validation rule but a field in a rules set that can conventionally be used for application specific data that is descriptive for the document field:
{'id': {'type': 'string', 'regex': r'[A-M]\d{,6}',
'meta': {'label': 'Inventory Nr.'}}}
The assigned data can be of any type.
New in version 1.3.
min, max¶
Minimum and maximum value allowed for any object whose class implements
comparison operations (__gt__
& __lt__
).
>>> schema = {'weight': {'min': 10.1, 'max': 10.9}}
>>> document = {'weight': 10.3}
>>> v.validate(document, schema)
True
>>> document = {'weight': 12}
>>> v.validate(document, schema)
False
>>> v.errors
{'weight': ['max value is 10.9']}
Changed in version 1.0: Allows any type to be compared.
Changed in version 0.7: Added support for float
and number
types.
minlength, maxlength¶
Minimum and maximum length allowed for sized types that implement __len__
.
>>> schema = {'numbers': {'minlength': 1, 'maxlength': 3}}
>>> document = {'numbers': [256, 2048, 23]}
>>> v.validate(document, schema)
True
>>> document = {'numbers': [256, 2048, 23, 2]}
>>> v.validate(document, schema)
False
>>> v.errors
{'numbers': ['max length is 3']}
noneof¶
Validates if none of the provided constraints validates the field. See *of-rules for details.
New in version 0.9.
nullable¶
If True
the field value is allowed to be None
. The rule will be
checked on every field, regardless it’s defined or not. The rule’s constraint
defaults False
.
>>> v.schema = {'a_nullable_integer': {'nullable': True, 'type': 'integer'}, 'an_integer': {'type': 'integer'}}
>>> v.validate({'a_nullable_integer': 3})
True
>>> v.validate({'a_nullable_integer': None})
True
>>> v.validate({'an_integer': 3})
True
>>> v.validate({'an_integer': None})
False
>>> v.errors
{'an_integer': ['null value not allowed']}
Changed in version 0.7: nullable
is valid on fields lacking type definition.
New in version 0.3.0.
*of-rules¶
These rules allow you to define different sets of rules to validate against.
The field will be considered valid if it validates against the set in the list
according to the prefixes logics all
, any
, one
or none
.
allof |
Validates if all of the provided constraints validates the field. |
anyof |
Validates if any of the provided constraints validates the field. |
noneof |
Validates if none of the provided constraints validates the field. |
oneof |
Validates if exactly one of the provided constraints applies. |
Note
Normalization cannot be used in the rule sets within the constraints of these rules.
Note
Before you employ these rules, you should have investigated other possible solutions for the problem at hand with and without Cerberus. Sometimes people tend to overcomplicate schemas with these rules.
For example, to verify that a field’s value is a number between 0 and 10 or 100 and 110, you could do the following:
>>> schema = {'prop1':
... {'type': 'number',
... 'anyof':
... [{'min': 0, 'max': 10}, {'min': 100, 'max': 110}]}}
>>> document = {'prop1': 5}
>>> v.validate(document, schema)
True
>>> document = {'prop1': 105}
>>> v.validate(document, schema)
True
>>> document = {'prop1': 55}
>>> v.validate(document, schema)
False
>>> v.errors
{'prop1': ['no definitions validate',
{'anyof definition 0': ['max value is 10'],
'anyof definition 1': ['min value is 100']}]}
The anyof
rule tests each rules set in the list. Hence, the above schema is
equivalent to creating two separate schemas:
>>> schema1 = {'prop1': {'type': 'number', 'min': 0, 'max': 10}}
>>> schema2 = {'prop1': {'type': 'number', 'min': 100, 'max': 110}}
>>> document = {'prop1': 5}
>>> v.validate(document, schema1) or v.validate(document, schema2)
True
>>> document = {'prop1': 105}
>>> v.validate(document, schema1) or v.validate(document, schema2)
True
>>> document = {'prop1': 55}
>>> v.validate(document, schema1) or v.validate(document, schema2)
False
New in version 0.9.
*of-rules typesaver¶
You can concatenate any of-rule with an underscore and another rule with a list of rule-values to save typing:
{'foo': {'anyof_regex': ['^ham', 'spam$']}}
# is equivalent to
{'foo': {'anyof': [{'regex': '^ham'}, {'regex': 'spam$'}]}}
# but is also equivalent to
# {'foo': {'regex': r'(^ham|spam$)'}}
Thus you can use this to validate a document against several schemas without implementing your own logic:
>>> schemas = [{'department': {'required': True, 'regex': '^IT$'}, 'phone': {'nullable': True}},
... {'department': {'required': True}, 'phone': {'required': True}}]
>>> emloyee_vldtr = Validator({'employee': {'oneof_schema': schemas, 'type': 'dict'}}, allow_unknown=True)
>>> invalid_employees_phones = []
>>> for employee in employees:
... if not employee_vldtr.validate(employee):
... invalid_employees_phones.append(employee)
oneof¶
Validates if exactly one of the provided constraints applies. See *of-rules for details.
New in version 0.9.
readonly¶
If True
the value is readonly. Validation will fail if this field is
present in the target dictionary. This is useful, for example, when receiving
a payload which is to be validated before it is sent to the datastore. The
field might be provided by the datastore, but should not writable.
A validator can be configured with the initialization argument
purge_readonly
and the property with the same name to let it delete all
fields that have this rule defined positively.
Changed in version 1.0.2: Can be used in conjunction with default
and default_setter
,
see Default Values.
regex¶
The validation will fail if the field’s value does not match the provided regular expression. It is only tested on string values.
>>> schema = {
... 'email': {
... 'type': 'string',
... 'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
... }
... }
>>> document = {'email': 'john@example.com'}
>>> v.validate(document, schema)
True
>>> document = {'email': 'john_at_example_dot_com'}
>>> v.validate(document, schema)
False
>>> v.errors
{'email': ["value does not match regex '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$'"]}
A trailing $
is ensured for all patterns in order to encourage users to
write complete patterns for matching (and not a searching) strings. The
implementation is inconsistent with regards to a leading ^
, these are not
enforced. That inconsistency will not be fixed for the 1.3.x
release
series.
For details on regular expression syntax, see the documentation on the standard
library’s re
-module.
Hint
Mind that one can set behavioural flags as part of the expression which is
equivalent to passing flags
to the re.compile()
function for
example. So, the constraint '(?i)holy grail'
includes the equivalent
of the re.I
flag and matches any string that includes ‘holy grail’
or any variant of it with upper-case glyphs. Look for (?aiLmsux)
in the
mentioned library documentation for a description there.
New in version 0.7.
require_all¶
This can be used in conjunction with the schema rule
when validating a mapping in order to set the
require_all
property of the validator for the
subdocument.
For a full elaboration refer to this paragraph.
required¶
If True
the field is mandatory. Validation will fail when it is missing,
unless validate()
is called with update=True
:
>>> v.schema = {'name': {'required': True, 'type': 'string'}, 'age': {'type': 'integer'}}
>>> document = {'age': 10}
>>> v.validate(document)
False
>>> v.errors
{'name': ['required field']}
>>> v.validate(document, update=True)
True
Note
To define all fields of a document as required see this section about the available options.
Note
String fields with empty values will still be validated, even when
required
is set to True
. If you don’t want to accept empty values,
see the empty rule.
Note
The evaluation of this rule does not consider any constraints defined with the dependencies rule.
Changed in version 0.8: Check field dependencies.
schema¶
A given mapping as value will be validated against the schema that is provided as constraint.
>>> schema = {'a_dict':
... {'type': 'dict',
... 'schema':
... {'address': {'type': 'string'},
... 'city': {'type': 'string', 'required': True}}
... }}
>>> document = {'a_dict': {'address': 'my address', 'city': 'my town'}}
>>> v.validate(document, schema)
True
Note
To validate arbitrary keys of a mapping, see keysrules-rule, resp. valuesrules-rule for validating arbitrary values of a mapping.
type¶
Tests whether the field value’s type matches (one of) the specified type(s). There are several ways a type can be specified, each with dis-/advantages regarding different usage aspects:
- any object like classes or abstract types that can be used as second argument
to the builtin’s
isinstance()
function - strings that reference either one of the named,
- strict types whose mapping to actual types are documented in the table below
- abstract types that map to the types from the
collections.abc
module - their extend depends on the Python version - these are are camel-cased (e.g.Set
orMutableMapping
) - custom types that can be defined per
Validator
class
- generic aliases from the
typing
module, including compound types- type parameters that are given as string have Cerberus’ semantics of named
types and are not resolved like static type checkers do, e.g.
Set["string"]
is a valid type specification
- type parameters that are given as string have Cerberus’ semantics of named
types and are not resolved like static type checkers do, e.g.
Named types allow the serialization of schemas and the exclusion of particular subtypes.
Type Name | Python Type |
---|---|
boolean |
bool |
bytesarray |
bytearray |
bytes |
bytes |
complex |
complex |
date |
datetime.date , but not its subclass datetime.datetime |
datetime |
datetime.datetime |
dict |
dict |
float |
float |
frozenset |
frozenset |
integer |
int , but not its subclass bool |
list |
list |
number |
float , int , but not bool |
set |
set |
string |
str |
tuple |
tuple |
type |
type (classes) |
Here are examples of the different ways to specify a type:
>>> document = {"items": frozenset(("a", "b", "c"))}
>>> # class-based test
>>> v.schema = {"items": {"type": frozenset}}
>>> v.validate(document)
True
>>> # named concrete type
>>> v.schema = {"items": {"type": 'frozenset'}}
>>> v.validate(document)
True
>>> # also a named concrete type
>>> v.schema = {"items": {"type": 'set'}}
>>> v.validate(document)
False
>>> # named abstract type
>>> v.schema = {"items": {"type": 'Set'}}
>>> v.validate(document)
True
>>> import typing
>>> # compound type
>>> v.schema = {"items": {"type": typing.Set[int]}}
>>> v.validate(document)
False
>>> # compound type with Cerberus' semantics for strings
>>> v.schema = {"items": {"type": typing.Set["integer"]}}
>>> v.validate(document)
False
A list of types can be used to allow different values of different types:
>>> v.schema = {'quotes': {'type': ['string', list]}}
>>> v.validate({'quotes': 'Hello world!'})
True
>>> v.validate({'quotes': ['Do not disturb my circles!', 'Heureka!']})
True
>>> v.schema = {'quotes': {'type': ['string', 'list'],
... 'itemsrules': {'type': 'string'}}
... }
>>> v.validate({'quotes': 'Hello world!'})
True
>>> v.validate({'quotes': [1, 'Heureka!']})
False
>>> v.errors
{'quotes': [{0: ["must be one of these types: ('string',)"]}]}
Note
Please note that type validation is performed before most others which exist for the same field (only nullable and readonly are considered beforehand). In the occurrence of a type failure subsequent validation rules on the field will be skipped and validation will continue on other fields. This allows one to safely assume that field type is correct when other (standard or custom) rules are invoked.
Changed in version 1.0: Added the binary
data type.
Changed in version 0.9: If a list of types is given, the key value must match any of them.
Changed in version 0.7.1: dict
and list
typechecking are now performed with the more generic
Mapping
and Sequence
types from the builtin collections
module.
This means that instances of custom types designed to the same interface as
the builtin dict
and list
types can be validated with Cerberus. We
exclude strings when type checking for list
/Sequence
because it
in the validation situation it is almost certain the string was not the
intended data type for a sequence.
Changed in version 0.7: Added the set
data type.
Changed in version 0.6: Added the number
data type.
Changed in version 0.4.0: Type validation is always executed first, and blocks other field validation rules on failure.
Changed in version 0.3.0: Added the float
data type.
valuesrules¶
This rules takes a set of rules as constraint that all values of a mapping are validated with.
>>> schema = {'numbers':
... {'type': 'dict',
... 'valuesrules': {'type': 'integer', 'min': 10}}
... }
>>> document = {'numbers': {'an integer': 10, 'another integer': 100}}
>>> v.validate(document, schema)
True
>>> document = {'numbers': {'an integer': 9}}
>>> v.validate(document, schema)
False
>>> v.errors
{'numbers': [{'an integer': ['min value is 10']}]}
New in version 0.7.
Changed in version 0.9: renamed keyschema
to valueschema
Changed in version 1.3: renamed valueschema
to valuesrules