Skip to content

How do you create a dependency between two fields with constraints ? #622

@aepli

Description

@aepli

How to create a dependency between two fields where the second field is mandatory only if the first field contains a specific value ?

Why don't I get a validation error when the second field is missing ?

In this example, I get True when I was expecting False:

>>> import sys
>>> print(sys.version_info)
sys.version_info(major=3, minor=9, micro=2, releaselevel='final', serial=0)
>>> import cerberus
>>> cerberus.__version__
'1.3.7'
>>> v = cerberus.Validator()
>>> schema = {'field1': {'type': 'string', 'allowed': ['text', 'choice']},
...           'field2': {'dependencies': {'field1': 'choice'}, 'type': 'number'}
... }
>>> document = {'field1': 'choice', 'field2': 1}
>>> v.validate(document, schema)
True
>>> del(document['field2'])
>>> v.validate(document, schema)
True

How do you create dependency between these two fields ?
Thank you for your help.

Regards.

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