使用Cerberus进行依赖关系验证

我正在使用Cerberus验证CSV文件,但在我认为是一些基本逻辑的情况下却遇到了麻烦

场景:

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上检查了提出的问题,但似乎找不到任何明显的解决方案。

罗曼·佩列赫雷斯特

注意
此规则(dependencies的评估考虑该required规则定义的任何约束

不管是什么"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#dependencies


更新

您的条件的解决方案“如果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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Cerberus根据条件进行依赖关系验证

无法使用IServerSideEvents依赖关系进行解析

Cerberus依赖关系如何引用文档中较高的字段?

Webpack:使用其他依赖关系而不是其自身的子依赖关系进行依赖

验证Maven的依赖关系管理

如何使用StanfordNLP Python包进行依赖关系解析?

如何使用结构/接口模拟依赖关系进行测试

如何使用Java进行拓扑排序(依赖关系解析)

使用Roslyn进行编译时自动解决依赖关系

“ rpm --test”是否也验证依赖关系?

Gradle依赖关系:使用本地jar进行编译还是通过Maven存储库进行编译?

如何使用Symfony组件配置对自定义验证器的依赖关系?

对树的直接 spaCy 依赖关系进行排名

Bower依赖关系以进行验收测试

使用工件的依赖关系

使用Unity解决依赖关系

使用依赖关系构建Maven

使用 NestJS 解决依赖关系

如何使用npm更新依赖关系的依赖关系

使用Charm依赖关系> 4.3.3进行POST时,Gluon Connect返回NPE

golang和godep:使用godep时,在golang依赖关系更新后进行Build \ install安装?

使用Kitchen and Chef在本地进行开发时,如何模拟OpsWorks特定的服务/依赖关系?

使用make进行构建时,ASM文件的“循环依赖关系下降”

Spring进行依赖注入的最小依赖关系是什么?

Cerberus META验证规则访问

是否可以使用依赖关系遍历程序通过静态库来验证dll正在使用的链接函数?

在验证程序中使用自动装配的依赖项(例如会话)进行测试

使用正确的数据类型时,使用 Cerberus 验证 JSON 架构会引发错误

VS 依赖关系验证图:超级和子命名空间