Skip to content

NotImplementedError when accessing validator errors while using oneof list of dict #299

Closed

Description

@kardaj

Use-case abstract

When I run the following snippet, using the 1.1 version, I get a NotImplementedError

from cerberus import Validator
schema = {
    'x': {
        "type": "dict",
        "oneof": [{
            "schema": {
                'v': {'type': 'string'}
            }}, {
            "schema": {
                'v': {'type': 'list', 'schema': {'type': 'dict', 'schema': {
                    'a': {'type': 'string', 'required': True}
                }}},
            }}]
    }}

v = Validator(schema)
v.validate({'x': {'v': ['a']}})
v.errors

In this example, the problem is that a is supposed to be a dict. Instead, a string was found.

Support request

After looking into the code, I found out it's due to following section in the insert_logic_error method of the BasicErrorHandler.

for child_error in child_errors:
    if child_error.is_logic_error:
        raise NotImplementedError
    elif child_error.is_group_error:
        raise NotImplementedError

Replacing it with the next snippet fixes my problem without breaking any of the existing tests of the project.

for child_error in child_errors:
    if child_error.is_logic_error:
        self.insert_logic_error(child_error)
    elif child_error.is_group_error:
        self.insert_group_error(child_error)

But I guess this behavior is not an accident and there's a reason for the NotImplementedError being there. Am I missing something?

Activity

  1. funkyfuture commented on Feb 24, 2017

    @funkyfuture
    Member

    see #278

  2. boramalper commented on Mar 15, 2017

    @boramalper

    Same issue here! Can be reproduced using:

    import cerberus
    
    generic_message_schema = {
        b"t": {"type": "binary", "empty": False, "required": True},
    }
    
    common_response_schema = {
        b"y": {"type": "binary", "allowed": [b"r"], "required": True}
    }
    
    common_response_arguments_schema = {
        b"id": {"type": "binary", "minlength": 20, "maxlength": 20, "required": True}
    }
    
    get_peers_response_schema = {
        **generic_message_schema,
        **common_response_schema,
        b"r": {"required": True, "schema": {
            **common_response_arguments_schema,
            b"token": {"type": "binary", "empty": False, "required": True},
            b"values": {"type": "list", "excludes": b"nodes", "required": True, "schema": {
                "type": "bytes",
                "minlength": 6, "maxlength": 6
            }},
            b"nodes": {"type": "binary", "excludes": b"values", "required": True, "minlength": 26,
                        "maxlength": 8 * 26}
        }}
    }
    
    print(">>>", cerberus.Validator().validate({}, get_peers_response_schema))
    $ pip3 show cerberus
    Name: Cerberus
    Version: 1.1
    Summary: Lightweight, extensible schema and data validation tool for Python dictionaries.
    Home-page: http://github.com/nicolaiarocci/cerberus
    Author: Nicola Iarocci
    Author-email: nicola@nicolaiarocci.com
    License: ISC
    Location: /home/bora/.pyenv/versions/3.6.0/lib/python3.6/site-packages
    Requires: 
    
  3. added a commit that references this issue on Jun 28, 2017
    6fe0e3d
  4. drcraig commented on Aug 10, 2017

    @drcraig

    Any chance of the fix for this being released soon? I'm hitting exactly this error in what is really a perfect use case for a one-of rule, and am struggling to figure out a way to refactor my schema to avoid it.

  5. added a commit that references this issue on Sep 27, 2017
    d560d1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions