diff --git a/cerberus/tests/test_validation.py b/cerberus/tests/test_validation.py index 693fa8bb..821d0277 100644 --- a/cerberus/tests/test_validation.py +++ b/cerberus/tests/test_validation.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- +import itertools import re import sys from datetime import datetime, date from random import choice from string import ascii_lowercase -import itertools from pytest import mark diff --git a/docs/usage.rst b/docs/usage.rst index 61b5e320..d43e1b1b 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -168,37 +168,12 @@ Requiring all ------------- See also :ref:`this paragraph `, :ref:`this paragraph `. -By default any keys defined in the schema are not required: - -.. doctest:: - - >>> schema = {'name': {'type': 'string', 'maxlength': 10}} - >>> v.validate({}, schema) - True - +By default any keys defined in the schema are not required. However, you can require all document keys pairs by setting -``require_all`` to ``True``: - -.. doctest:: - - >>> v.schema = {'name': {'type': 'string', 'maxlength': 10}} - >>> v.require_all = True - >>> v.validate({}) - False - -``require_all`` can also be set at initialization: - -.. doctest:: - - >>> v = Validator({'name': {'type': 'string', 'maxlength': 10}}, require_all=True) - >>> v.validate({}) - False - >>> v.require_all = False - >>> v.validate({}) - True - -``require_all`` can also be set as rule to configure a validator for a nested -mapping that is checked against the :ref:`schema ` rule: +``require_all`` to ``True`` at validator initialization (``v = Validator(…, require_all=True)``) +or change latter via attribute access (``v.require_all = True``). +``require_all`` can also be set as rule to configure a validator for a subdocument +that is checked against the :ref:`schema ` rule: .. doctest:: @@ -210,15 +185,17 @@ mapping that is checked against the :ref:`schema ` rule: ... 'name': {'type': 'string'}, ... 'a_dict': { ... 'type': 'dict', - ... 'require_all': True, # this overrides the behaviour for - ... 'schema': { # the validation of this definition + ... 'require_all': True, + ... 'schema': { ... 'address': {'type': 'string'} ... } ... } ... } - >>> v.validate({'a_dict': {}}, schema) + >>> v.validate({'name': 'foo', 'a_dict': {}}, schema) False + >>> v.errors + {'a_dict': [{'address': ['required field']}]} >>> v.validate({'a_dict': {'address': 'foobar'}}, schema) True diff --git a/docs/validation-rules.rst b/docs/validation-rules.rst index 9b8560fa..c0fa45c8 100644 --- a/docs/validation-rules.rst +++ b/docs/validation-rules.rst @@ -674,7 +674,6 @@ For a full elaboration refer to :ref:`this paragraph `. required -------- -See also :ref:`this paragraph `. If ``True`` the field is mandatory. Validation will fail when it is missing, unless :meth:`~cerberus.Validator.validate` is called with ``update=True``: @@ -691,6 +690,10 @@ unless :meth:`~cerberus.Validator.validate` is called with ``update=True``: >>> v.validate(document, update=True) True +.. note:: + + To define all fields of a document as required see :ref:`this paragraph `. + .. note:: String fields with empty values will still be validated, even when