New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kardaj opened this Issue Feb 23, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@kardaj
Copy link

kardaj commented Feb 23, 2017

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?

@funkyfuture

This comment has been minimized.

Copy link
Member

funkyfuture commented Feb 24, 2017

see #278

@boramalper

This comment has been minimized.

Copy link

boramalper commented Mar 15, 2017

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: 
@drcraig

This comment has been minimized.

Copy link

drcraig commented Aug 10, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment