使用 Cerberus 进行依赖项验证

2024-04-26

我正在验证 CSV 文件Cerberus http://docs.python-cerberus.org/en/stable/index.html但我正在努力解决我认为的一些基本逻辑

设想:

CSV 文件有 2 列。Column 2仅当以下情况时才需要有一个值Column 1有一个价值。如果Column 1那么是空的Column 2也应该为空。

我认为这将是最直接的规则之一,但到目前为止,一切都没有按预期进行。

下面是使用 python 字典的相同逻辑。

from cerberus import Validator
v = Validator()

schema = {
    "col1": {"required": False},
    "col2": {"required": True, "dependencies": "col1"},
}

document = {
    "col1": "a",
    "col2": ""
}

v.validate(document, schema)  # This responds with True!? Why?
v.errors
{}

我预计会出现错误Column 2在这里因为Column 1已提供,但结果是True意思是没有错误

我已经检查过加注github 上的问题 https://github.com/pyeve/cerberus/issues但似乎找不到任何明显的解决方案。


Note
这条规则的评价(dependencies) does not考虑用定义的任何约束required rule.

不管怎样"required"将会:

from cerberus import Validator
v = Validator()

document = {
    "col1": "a",
    "col2": ""
}

schema = {
    "col1": {"required": False},
    "col2": {"required": True, "dependencies": "col1"},
}

print(v.validate(document, schema))  # True
print(v.errors) # {}

schema = {
    "col1": {"required": True},
    "col2": {"required": True, "dependencies": "col1"},
}


print(v.validate(document, schema))  # True
print(v.errors)  # {}

schema = {
    "col1": {"required": True},
    "col2": {"required": False, "dependencies": "col1"},
}


print(v.validate(document, schema))  # True
print(v.errors)  # {}

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


Update:

针对您的情况的解决方案“如果 col1 中有值,则强制 col2。".
要应用复杂的规则 - 创建自定义验证器如下所示:

from cerberus import Validator


class MyValidator(Validator):
    def _validate_depends_on_col1(self, depends_on_col1, field, value):
        """ Test if a field value is set depending on `col1` field value.
        """
        if depends_on_col1 and self.document.get('col1', None) and not value:
            self._error(field, f"`{field}` cannot be empty given that `col1` has a value")


v = MyValidator()

schema = {
    "col1": {"required": False},
    "col2": {"required": True, "depends_on_col1": True},
}

print(v.validate({"col1": "a", "col2": ""}, schema))  # False
print(v.errors) # {'col2': ['`col2` cannot be empty given that `col1` has a value']}

print(v.validate({"col1": "", "col2": ""}, schema))  # True
print(v.errors) # {}

print(v.validate({"col1": 0, "col2": "aaa"}, schema))  # True
print(v.errors) # {}

注意,您需要遵守什么列的约定col1值应被视为空(以调整自定义验证器规则)。


指定“依赖项”字段名称的扩展版本:

class MyValidator(Validator):
    def _validate_depends_on_col(self, col_name, field, value):
        """ Test if a field value is set depending on `col_name` field value.
        """
        if col_name and self.document.get(col_name, None) and not value:
            self._error(field, f"`{field}` cannot be empty given that `{col_name}` has a value")


v = MyValidator()

document = {"col1": "a", "col2": ""}

schema = {
    "col1": {"required": False},
    "col2": {"required": True, "depends_on_col": "col1"},
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Cerberus 进行依赖项验证 的相关文章

随机推荐