question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'dependencies' incorrectly validated by not-required field

See original GitHub issue
from cerberus import Validator
schema = {
        'feature': {
        'type': 'dict',
        'default_setter': lambda _: {},
        'required': False,
        'schema': {
            'enabled': {
                'type': 'boolean',
                'required': False,
                'default': False
            },
            'subfeature': {
                'required': False,
                'type': 'integer',
                'nullable': True,
                'default': None,
                'dependencies': {'^feature.enabled' : True}
            }
        }
    },
}

v = Validator()
v.validate({'feature' : {'enabled' : True}}, schema) # expected True, actual True
v.validate({'feature' : {'enabled' : False}}, schema)  # expected True, actual False
v.validate({'feature' : {'enabled' : False, 'subfeature' : None}}, schema)  # expected True, actual False
v.validate({'feature' : {'enabled' : True, 'subfeature' : 3}}, schema)  # expected True, actual True
v.validate({'feature' : {'enabled' : False, 'subfeature' : 3}}, schema)  # expected True, actual False

Used Cerberus version / latest commit:1.3.2 (from pip)

  • 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.

  • My question does not concern a practical use-case that I can’t figure out to solve.


Use-case abstract

I have a schema in which the main dict named ‘feature’ is optional and has a optional boolean field named “enabled”. This dict has a nested dict named ‘subfeature’ which is also optional and depends on the parent dict’s ‘enabled’ field to be ‘True’ as a validation. Meaning the subfeature cant be set if the feature is not.


Bug report / Feature request

The problem is that when the main ‘feature.enabled’:False occurs, the nested dict which is not being set complains the feature.enabled is not True. This shouldnt happen because the nested dict is optional and is not being assigned, so it shouldnt validate. If the subfeature was required, than this behavior would make sense, but for optional fields, this is unexpected.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
funkyfuturecommented, Sep 29, 2020

i can reproduce the bug and i stripped the test case down so far that the required rule plays no role (except the default mentioned in the summary).

@mark.parametrize(
    "document",
    [
        {"enabled": True},
        {"enabled": False},
        {'enabled': False, 'subfeature': None},
        {'enabled': True, 'subfeature': 3},
        {'enabled': False, 'subfeature': 3},
    ],
)
def test_issue_553(document):
    # https://github.com/pyeve/cerberus/issues/553
    schema = {
        "enabled": {},
        "subfeature": {
            "nullable": True,
            # removing this eliminates one error:
            "default": None,
            "dependencies": {"enabled": True},
        },
    }
    assert_success(document, schema)
0reactions
funkyfuturecommented, Oct 4, 2020

i have looked around on SO and didn’t find a well-fitting question/answer. i also really can’t guess where your expectations diverge from the implementations.

let me try to point one thing out again: as you define a dfault for subfeature, the field will always be present in the document. the depenedencies rule defined on that field always expects the enabled field to be True. hence all documents where it holds False are evaluated as invalid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with required and dependencies · Issue #274
Also, if dependencies are declared for the field, its required rule will only be validated if all dependencies are included with the document....
Read more >
please double check all required fields - Trailblazer Community
Validation Error on Asset deletion with dependencies default to incorrect message "Invalid data - please double check all required fields".
Read more >
Jquery validation Plugin required field dependency
Using Jquery validation plugin, i am trying to validate a field companyid2 and display an error message if the user did not enter...
Read more >
Error publishing workspace - Field Dependency configuration ...
Solutions to a Product Problem ... Field Dependency configuration is not valid for field [FieldName] in Item [Ticket].
Read more >
PH29775: CIRCULAR DEPENDENCY VALIDATION ...
Description: When you?re writing field dependencies or dependent picklists, the validation of circular dependencies does not ignore the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found