- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下架构
schema = {
"value_type":{
"type": "string", "required": True
},
"units": {
"type": "string",
"dependencies": {"value_type": ["float", "integer"]},
"required": True
}
}
我希望仅当 value_type
字段的值为 float
或整数
。
这是我想要实现的行为
v = Validator(schema)
v.validate({"value_type": "float", "units": "mm"}) # 1.
True
v.validate({"value_type": "boolean", "units": "mm"}) # 2.
False
v.validate({"value_type": "float"}) # 3.
False
v.validate({"value_type": "boolean"}) # 4.
True
上述架构仅返回前 3 种情况的预期结果。
如果我将 units
的定义(通过省略 "required": True
)更改为
"units": {"type": "string", "dependencies": {"value_type": ["float", "integer"]}}
然后验证
v.validate({"value_type": "float"}) # 3.
True
返回True
,这不是我想要的。
我查看了 documentation 中的 oneof
规则但找不到将其仅应用于 required
属性的方法。
我希望仅当满足依赖关系时,required 的值才为 True
。
我应该如何修改我的架构来实现这一目标?
最佳答案
由于您的变体跨越多个字段,*of
规则并不完全适合,特别是因为这些似乎是文档中的顶级字段。
我通常建议仍然有Python,并不是所有的东西都必须用模式来表达,所以你可以简单地定义两个有效的模式并针对它们进行测试:
schema1 = {...}
schema2 = {...}
if not any(validator(document, schema=x) for x in (schema1, schema2)):
boom()
这也比您最终得到的任何模式更容易理解。
或者,您可以使用 check_with
规则。该示例显示了两种不同的提交错误的方式,其中当错误仅呈现给人类时,后一种方式是首选,因为它们允许针对不同情况的自定义消息,同时缺乏有关错误的结构信息:
class MyValidator(Validator):
def _check_with_units_required(self, field, value):
if value in ("float", "integer"):
if "units" not in self.document:
self._error("units", errors.REQUIRED_FIELD, "check_with")
else:
if "units" in self.document:
self._error(
"units", "The 'units' field must not be provided for value "
"types other than float or integer."
)
schema = {
"value_type": {
"check_with": "units_required",
"required": True,
"type": "string"
},
"units": {
"type": "string",
}
}
validator = MyValidator(schema)
assert validator({"value_type": "float", "units": "mm"})
assert not validator({"value_type": "boolean", "units": "mm"})
assert not validator({"value_type": "float"})
assert validator({"value_type": "boolean"})
关于python - Cerberus - 仅当满足依赖性时才需要字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54884269/
我真的很喜欢 Cerberus,但我无法在文档中找到一个简单的案例。我想要 fudge 类型,它是一个带有 ~ 前缀的字符串。我根本不知道该怎么做.. fudge_type = cerberus.Ty
我正在使用 cerberus 来验证数据。我的一个字段是可选的 - 不需要每一项都存在。但是,该键必须在整个数据数组中至少填充一次。 举个例子,假设我想验证键 'c' 出现在我的数据列表中的至少一个字
考虑以下架构 schema = { "value_type":{ "type": "string", "required": True }, "units":
我使用的是 Cerberus 版本 1.1。 Cerberus required 验证规则似乎默认为 False,结果是空文档完全有效。 >>> schema = { 'spam': {'ty
我想使用 Cerberus 来验证某个字段不存在于对象中。 我想使用类似的东西: my_schema = { 'normal_field': { 'type': 'string
我正在尝试为具有引用文档中较高字段的依赖项的文档创建架构。例如: document = { 'packages': { 'some-package': {'version':
来自documentation ,我不清楚自定义规则和自定义验证器在用例中的区别是什么。在文档中给出的示例中,唯一的区别是额外的 if 语句检查自定义规则中 is_odd 的值。我什么时候应该更喜欢自
我有一个以 ID 作为根键的字典,我想验证它。换句话说,我想要验证的字典的根键是动态的。有没有办法针对根 key 运行 key 架构? 例如https://repl.it/@crunk1/cerber
考虑以下 Cerberus 架构: { 'employee': { 'type': 'list', 'schema': { 'type': 'dict',
有没有办法拥有 Cerberus验证两个字段是否具有相同数量的元素? 例如,该文档将验证: {'a': [1, 2, 3], b: [4, 5, 6]} 这不会: {'a': [1, 2, 3], '
正在使用 Cerberus 验证 CSV 文件但我正在努力解决我认为的一些基本逻辑 场景: CSV 文件有 2 列。仅当Column 1 有值时,Column 2 才需要有值。如果第 1 列 为空,则
我知道根据 Cerberus documentation ,可以根据其他键值定义验证依赖项,例如: schema = {'field1': {'required': False},
我正在使用开源 Python 数据验证库 Cerberus验证字典的结构。我希望它获取部分无效的文档并在没有无效键的情况下输出它。 例如,对于这个脚本: from cerberus import Va
我正在使用 python 库 cerberus ( http://docs.python-cerberus.org/en/stable/ ),我想检查 JSON 字段是数字(整数)还是空字符串。 我尝
我有以下架构: schema = { 'person': { 'name': {'type': 'string', 'required': True, 'minlength':
我想本地化 Cerberus 返回的错误消息,例如我想实现以下目标: >>> validator.schema = {'animal': {'forbidden': ['Einhorn']}} >>>
我有一个很大的 json 文档,如果其他字段具有精确值,则应该需要其中一些字段。例如 document = {'is_realty_address': False, 'postcode': 11111
我试图将字符串强制为日期,以便它可以验证日期数据类型,但它仍然返回False: from cerberus import Validator from datetime import datetime
有没有办法告诉 cerberus 将模式中的所有键的 required 默认设置为 True?这会节省我一些时间,因为大多数时候我想断言 key 的存在。 最佳答案 我认为这个问题没有通用的解决方案,
正在验证 .csv 文件,我想以用户习惯的格式给出验证结果。要利用Cerberus ,我让用户在 .yaml 文件中定义验证规则。 schema.yaml Rules: Rule1: maxle
我是一名优秀的程序员,十分优秀!