Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Melentev authored and Nikita Melentev committed Aug 9, 2018
1 parent f0f37ac commit 9e4fcdb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cerberus/tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
43 changes: 10 additions & 33 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,37 +168,12 @@ Requiring all
-------------
See also :ref:`this paragraph <require_all>`, :ref:`this paragraph <required>`.

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 <schema_dict-rule>` 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 <schema_dict-rule>` rule:

.. doctest::

Expand All @@ -210,15 +185,17 @@ mapping that is checked against the :ref:`schema <schema_dict-rule>` 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
Expand Down
5 changes: 4 additions & 1 deletion docs/validation-rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ For a full elaboration refer to :ref:`this paragraph <requiring-all>`.

required
--------
See also :ref:`this paragraph <requiring-all>`.

If ``True`` the field is mandatory. Validation will fail when it is missing,
unless :meth:`~cerberus.Validator.validate` is called with ``update=True``:
Expand All @@ -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 <requiring-all>`.

.. note::

String fields with empty values will still be validated, even when
Expand Down

0 comments on commit 9e4fcdb

Please sign in to comment.