linclelinkpart5 · GitHub

This issue tracker is dedicated to bugs and feature requests.

Stack Overflow is the go-to place for general questions and general how-tos. On
SO, questions tagged with the 'cerberus' tag are actively monitored by the
project author, contributors and users.

When reporting a bug, please post an example and make it as simple as possible.
From your use-case throw everything out that is irrelevant for your question
/ isn't necessary to evoke the bug.

Try to adhere as much as possible to the template below:

Used Cerberus version / latest commit: ...

  • I consulted these documentations:
  • I consulted these sections of the docs (add more lines as necessary):
    • ...
    • ...
  • I found nothing relevant to my problem in the docs.
  • I found the documentation not helpful to my problem.
  • I have the capacity to improve the docs when my problem is solved.
  • I have the capacity to submit a patch when a bug is identified.

Use-case abstract

I am using a 'oneof_schema' rule in order to allow either:

  • an empty dictionary, or
  • a dictionary of a specific format

Support request / Bug report

Using: Cerberus 1.0.1 and Python 3.5.2

I'm seeing a strange error when trying to use a 'oneof_schema' rule for my document. The goal is to either allow the 'measurement' field to be an empty dict, or following a specific format.

Examples of documents that (should) pass:

{
    'tokens': [
        {
            'measurement': {
                'qty': { 'num': 10, 'den': 1, },
                'units': 'lbs',
            },
        },
    ],
}
{
    'tokens': [
        {
            'measurement': {
                'qty': 100,
                'units': 'lbs',
            },
        },
    ],
}
{
    'tokens': [
        {
            'measurement': {},
        },
    ],
}

Example of a document that should fail:

{
    'tokens': [
        {
            'measurement': {
                'qty':     { 'num': 'THIS WILL FAIL!', 'den': 1, },
                'units':    'lbs',
            },
        },
    ],
}

Interestingly, when I tested the above "should-fail" document, I get a strange exception:

$ python failure.py
Traceback (most recent call last):
  File "main.py", line 64, in <module>
    print(v.errors)
  File "/home/cheffu/venvs/cerberus-cheffu-test/lib/python3.5/site-packages/cerberus/validator.py", line 360, in errors
    return self.error_handler(self._errors)
  File "/home/cheffu/venvs/cerberus-cheffu-test/lib/python3.5/site-packages/cerberus/errors.py", line 443, in __call__
    self.extend(errors)
  File "/home/cheffu/venvs/cerberus-cheffu-test/lib/python3.5/site-packages/cerberus/errors.py", line 372, in extend
    self.add(error)
  File "/home/cheffu/venvs/cerberus-cheffu-test/lib/python3.5/site-packages/cerberus/errors.py", line 453, in add
    self.insert_group_error(error)
  File "/home/cheffu/venvs/cerberus-cheffu-test/lib/python3.5/site-packages/cerberus/errors.py", line 496, in insert_group_error
    self.insert_logic_error(error)
  File "/home/cheffu/venvs/cerberus-cheffu-test/lib/python3.5/site-packages/cerberus/errors.py", line 516, in insert_logic_error
    raise NotImplemented
TypeError: exceptions must derive from BaseException

If I change the rule to remove the 'oneof_schema' rule (losing the ability to accept an empty dict), I'm able to see the validation error, as expected:

$ python ok.py
{'tokens': [{0: [{'measurement': [{'qty': [{'oneof': ['none or more than one rule validate', {'oneof definition 1': ['must be of integer type'], 'oneof definition 0': ['must be of integer type']}]}]}]}]}]}

The only difference in the code between the two files is the 'oneof_schema' rule in the 'measurement' schema (lines 36-39 in failure.py). Is this an error on my part, or something more insiduous?

Attached are the two Python files: failure.py.txt and ok.py.txt.

Read the original on github.com ↗