Skip to content
Advertisement

Dependencies validation using Cerberus

Am validating a CSV file with Cerberus but am struggling with what I’d assume is some basic logic

Scenario:

A CSV file has 2 columns. Column 2 requires to have a value only if Column 1 has a value. If Column 1 is empty then Column 2 should also be empty.

Am thinking this would be one of the most straight forward rules to write but so far nothing is working as expected.

Below is the same logic using python dictionaries.

JavaScript

I would have expected an error for Column 2 here because Column 1 has been provided but here the result is True meaning no error

I’ve checked raised issues on github but can’t seem to find any obvious solution.

Advertisement

Answer

Note
The evaluation of this rule (dependencies) does not consider any constraints defined with the required rule.

Whatever the "required" would be:

JavaScript

JavaScript

JavaScript

http://docs.python-cerberus.org/en/stable/validation-rules.html#dependencies


Update:

Solution for your condition “Make col2 mandatory if col1 has a value in it.“.
To apply a sophisticated rules – create a custom Validator as shown below:

JavaScript

Note, you need to run into convention of what column col1 values should be treated as empty (to adjust a custom validator rules).


Extended version to specify a “dependancy” field name:

JavaScript

http://docs.python-cerberus.org/en/stable/customize.html

Advertisement